Let's say I have such code (and its meaning is like in C#):
class Foo<T>
{
public T my_field;
}
and later in code I have:
var foo = new Foo<int>();
foo.my_field = 5;
My problem starts with the Foo<int>
. What I currently do is I take type Foo<T>
and mapping T -> int
and clone the surface of that type. Surface because I skip init expressions, method/property bodies, but nevertheless I create something like this on the fly:
class Foo<int>
{
public int my_field;
}
and then continue as it would be the usual type. The good thing is it works but the bad thing is it works slowly -- profiler does not hestitate to point out it is the bottleneck.
The other idea I have is using something like a "view" which would consist of reference to generic type (here Foo<T>
) and mapping (here T -> int
) and then using it each time I reference instance of generic type. In case of foo
, I would go through such view to a field, I would get its type T
, and "filter" it back through the view to get finally the result: int
. I would like to avoid trial&error approach so...
My question is how instances of generic type are handled in compiler (not in runtime)? I mean for real :-).