How are generics implemented?

Posted by greenoldman on Programmers See other posts from Programmers or by greenoldman
Published on 2013-10-28T16:23:41Z Indexed on 2013/10/28 22:10 UTC
Read the original article Hit count: 290

Filed under:
|

This is the question from compiler internals perspective.

I am interested in generics, not templates (C++), so I marked the question with C#. Not Java, because AFAIK the generics in both languages differ in implementations.

When I look at languages w/o generics it is pretty straightforward, you can validate the class definition, add it to hierarchy and that's it.

But what to do with generic class, and more importantly how handle references to it? How to make sure that static fields are singular per instantiations (i.e. each time generic parameters are resolved).

Let's say I see a call:

var x = new Foo<Bar>();

Do I add new Foo_Bar class to hierarchy?


Update: So far I found only 2 relevant posts, however even they don't go into much details in sense "how to do it by yourself":

© Programmers or respective owner

Related posts about c#

Related posts about generics