4 ms·
> "The concept of generics is only possible with some sort of compile time reflection in any language" This is not true. * The standard implementation techniq
by noelwelsh 3y ago
> "The concept of generics is only possible with some sort of compile time reflection in any language"
This is not true.
* The standard implementation technique is to box everything (have a uniform in-memory representation for all types). Monomorphization[1], which is the compilation strategy used by Rust, C++, and I guess Zig, requires type knowledge at compile-time.
* Generic functions should not have any knowledge of the concrete types of the values they are passed, as this breaks an abstraction boundary. Theorems for free[2] is about this idea.
[1]: https://en.wikipedia.org/wiki/Monomorphization https://en.wikipedia.org/wiki/Monomorphization
[2]: https://www2.cs.sfu.ca/CourseCentral/831/burton/Notes/July14/free.pdf https://www2.cs.sfu.ca/CourseCentral/831/burton/Notes/July14...
- bestouff 3y agoHow can you call a method on a type if you don't know anything about it ? How do you even know the method exist without some dynamic casting of sorts ?
- noelwelsh 3y agoIt's not calling methods on a type that is the issue. It's passing parameters to a method or function when that method or function has a generic type for that parameter. In Rust this is something like fn foo<A>(a: A): A (which can only have one implementation, the identity, if you don't break the abstraction barrier provided by generic types.)
- dangitdangitdan 3y ago[flagged]
- Joker_vD 3y agoYou pass that method along with the value, so it's the caller's responsibility. At some point along the call stack there will be a monomorphic function which has to know the actual type it's calling its polymorphic callee with.