2009-06-22

Preparation for the second meeting of the c#ID Study Group Würzburg

The last meeting has been quite a long time ago because one of us three took a vacation of a couple of weeks. So here we are back again.

The topic of this meeting is chapter 3: Parameterized typing with generics.

As I am now in my 4th year of developing with C# 2 there have been few really new and stunning facts in this chapter. Still I’ve learned quit a good bit and I was able to pick up quite a couple of terms that help me clarify my understanding of how generics work.

Here are the main points that were very valuable to me:

  • The diagram on page 68 is really an eye opener as it explains the relationship between unbound generic types, constructed (generic) types and instances. I wont put a copy of this diagram here. Go get the book!
  • The way the runtime constructs a type from an unbound generic type for each different combination of the generic parameters.
  • The way comparison are performed with the == and != operators based on the constraints on the generic type parameters as explained on pp 82, 83.
  • Very interesting is the demonstration on page 87 that each constructed type has its own static constructor .
  • I cannot remember finding any hint, that there are no generic methods in C++. Which is obvious if you look at how templates are a king of "preprocessor on steroids". The generic parameters of generic methods are only available at runtime (are they really?) but not at compile time.
    Which leads me to another question: Are generic parameters of methods resolved based on the runtime or on the compile time type?
  • OMG: Covariance and Contravariance again!
  • On p. 103 the options for working arround the invariance of generic types. The last item talks about a generic helper method to convert the items in a collection. Here should be mentioned that there is a method System.Linq.Enumerable.Cast(IEnumberable) that does just that. I think a hint would be in order even if this C# 3 features are dealt with later in the book.

2 comments:

Andrei Pamula said...

Hi Ralf,

what book are you studying? (I could not figure it from the post).

The diagram on page 68 is really an eye opener as it explains the relationship between unbound generic types, constructed (generic) types and instances.


Martin Odersky of Scala fame calls generics 'type constructors'.


Are generic parameters of methods resolved based on the runtime or on the compile time type?


On compile time. The compiler has to guarantee the 'soundness' of your type system.

Covariance & Contravariance is fun. There are some good explanations on the Scala site (the articles page) on how do they manage that vs. java + and a talk from Joshua Bloch.
google for: 'effective java pecs'.
...or some blog entries from the C# compiler guys. (you could fish them out from my delicious)

I think you might also enjoy the 'expression problem' as a 'study group quiz' (http://delicious.com/andrei.pamula/expression+problem)
(Mads Torgens discusses a solution in C# as well).

Ralf said...

Hi Andrei,
sorry, I didn't really expect someone to read this.
We are looking into "C# In Depth" by Jon Skeet (of stackoverflow fame).
We have just started and gather our notes at http://csharp-in-depth-wuerzburg.blogspot.com/.
Thanks for your hints.
ralf

Whos.amung.us?

whos online

Ralf"s shared items