Re:How generics were added to .NET
by kelnos on Tuesday March 06, 2018 @04:13AM
(#16527770)
At the VM/bytecode level, not much (if anything?). The benefit comes at compile-time, where the compiler can verify that you're putting the correct types into a collection, and that you're expecting the correct types out.
[ Parent
| on HN ]
Re:How generics were added to .NET
by SideburnsOfDoom on Tuesday March 06, 2018 @04:29AM
(#16527833)
> At the VM/bytecode level, not much (if anything?).
I don't think this is correct. In C# code at runtime, and in the bytecode, you can inspect types for metadata about their generic type params. It is present in the .Net bytecode.
See https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...
> When a generic type or method is compiled into (bytecode), it contains metadata that identifies it as having type parameters.
from https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...
But C# generics aren't erasure model collections are they?
Re:How generics were added to .NET
by SideburnsOfDoom on Tuesday March 06, 2018 @07:44AM
(#16528442)
No, they are not. The point is, it is different at the bytecode level.
You've got that completely the wrong way around. An erasing compiler has full access to the type (obviously - it's doing the erasing) and so can verify whether you're putting the correct types in or out of a collection.
Where erasure can cause issues is at runtime. For example:
X isa List<Int>
Even if X is indeed a list of ints that information isn't held at runtime, so the test can't be supported.Having said that in some respects this is a C# / Java ecumenical matter. Haskell, for example, doesn't bake types into output code so I suppose it erases even more that java, but it supports very rich polymorphic behaviour.