logoalt Hacker News

Lvl999Nooblast Wednesday at 4:52 AM2 repliesview on HN

Monomorphization has got nothing to do with type system though. If you have a GC (as go does), you can automatically box your references and go from a `impl Trait` to a `&mut dyn Trait` with the GC taking care of value vs reference semantics. Monomorphization is orthogonal to how you define the set of valid arguments.


Replies

Meroviuslast Wednesday at 10:08 AM

Except if your traits are not dyn-compatible. Which I believe a lot of Rust's traits are not. That restriction is specifically why Go does not allow methods to have extra type parameters: To make it possible for the language implementation to choose its own tradeoff between monomorphization and boxing.

So I don't think you can say that this has nothing to do with the type system. Here is a restriction in the Go type system that was specifically introduced to allow a broad range of implementation choices. To avoid being forced to choose slow compilers or slow code: https://research.swtch.com/generic

The Go type system and the way it does generics is directly designed to allow fast compile times.

munificentlast Wednesday at 11:02 PM

> you can automatically box your references

Yes, but that is now a different runtime cost which Go also didn't want to pay.

The language goes to great pains to give you pretty good control over layout in memory and avoid the "spray of tiny objects on the heap with pointers between them" that you get in Java and most other managed languages.

I think Swift maybe does something more clever with witness tables, but I don't recally exactly how it works.

It's not an easy problem.

show 1 reply