3 ms·
Isn't generics just a part of the compiler?
by gregoryl 6y ago
Isn't generics just a part of the compiler?
- ylyn 6y agoNo, .NET generics are real and are reified at runtime, unlike Java where generics are just some compile-time typechecking.
- miguelrochefort 6y agoOn platforms that don't allow runtime code generation, such as iOS, generics are actually compiled ahead of time. It's reasonable to ask if the same is done with NanoFramework.
- gregoryl 6y agoTo clarify, I was under-educated on how generics work, rather than being this clever.
- sesm 6y agoDoes this imply that generics in Ocaml/Haskell are not real?
- sideeffffect 6y ago"Generics" in Haskell are real, but they're not what is in the .NET lingo called reified. Just like Java does not monomorphise. On the other hand, C#, F# or Rust monomorphise (have reified generics).
- DaiPlusPlus 6y agoAre “monomorphism” and “reification” interchangeable terms though? Considering how .NET now has covariant/contravariant generics I wonder if it could be described as having “polymorphic” generics as opposed to monomorphic generics - (unrelated to OOP polymorphism, ofc).
- sideeffffect 6y agoAFAIUI _monomorphisation_ and reification are interchangable. That means, that if I have a polymorphic (generic) class `MyClassA<T>`, the compiler (regardless if it is JIT in the runtime as in C#, or AOT as in Rust) monomorphises (reifies) `MyClassA` for each instantiation of `T`. So `MyClassA<MyClassB>` and `MyClassA<MyClassC>` are distinct classes. For this reason, co-/contra-variance in C# works only for interfaces. Type parameters in classes are invariant. `MyClassA<MyClassB>` and `MyClassA<MyClassC>` are distinct classes, neither sub-/super-class of the other, even if `MyClassB` were a subclass of `MyClassC`.
- SigmundA 6y agoMy understanding is it doesn't matter in Haskell since it lacks reflection, they are real because you can't reflect and see they have been type erased at runtime. In C# and Java reflection is used extensively and the generics still exist in C# when reflecting they are gone in Java.