I know that Java implements parametric polymorphism (Generics) with erasure. I understand what erasure is.
I know that C# implements parametric polymorphism with reification. I know that can make you write
public void dosomething(List<String> input) {}
public void dosomething(List<Int> input) {}
or that you can know at runtime what the type parameter of some parameterised type is, but I don't understand what it is.
Reification is the process of taking an abstract thing and creating a concrete thing.
The term reification in C# generics refers to the process by which a generic type definition and one or more generic type arguments (the abstract thing) are combined to create a new generic type (the concrete thing).
To phrase it differently, it is the process of taking the definition of List<T>
and int
and producing a concrete List<int>
type.
To understand it further, compare the following approaches:
In Java generics, a generic type definition is transformed to essentially one concrete generic type shared across all allowed type argument combinations. Thus, multiple (source code level) types are mapped to one (binary level) type - but as a result, information about the type arguments of an instance is discarded in that instance (type erasure).
In C# generics, the generic type definition is maintained in memory at runtime. Whenever a new concrete type is required, the runtime environment combines the generic type definition and the type arguments and creates the new type (reification). So we get a new type for each combination of the type arguments, at runtime.
System.Type
class (even if the particular generic type argument combination you're instantiating didn't appear in your source code directly).In C++ templates, the template definition is maintained in memory at compile time. Whenever a new instantiation of a template type is required in the source code, the compiler combines the template definition and the template arguments and creates the new type. So we get a unique type for each combination of the template arguments, at compile time.