August 26, 2004 1:58 pm (permalink)

Satisfying and not satisfying constraints

Spent some more time on generic constraints. Some things are a bit surprising at first sight and so my first attempt to implement this was wrong:

interface IHello<t> { } class Foo<t> { public void Test<u> (IHello<u> hello) where U : T { } public void Ok<s> (IHello<s> hello) where S : T { Test (hello); } }

This example is ok and allowed - you may pass hello to that function since the constraints match. However, the following is not allowed:

class Hello<t> { } class Foo<t> { public static void Test<u> (Hello<u> a) where U : T { } } class X { } class Y : X { } class Z : Y { static void Main () { Hello<z> hello = new Hello<z> (); Foo<x>.Test<y> (hello); } }

The constraints just say that Y must derive from X - but this function still takes a Hello<Y> argument. You cannot interpret the constraints in a way that any generic instance Hello<T> is allowed, if just T derives from Y.

Posted by martin at August 26, 2004 1:58 pm.

August 25, 2004 4:27 pm (permalink)

Constraints and interfaces

Spent most of the day working on generic methods in interfaces and their constraints. If you have a generic method in an interface and you explicitly implement this interface method, you may not specify any constraints on that implementation - they're automatically inherited from the declaration in the interface:

public interface IFoo<t> { void Hello<u> (T t, U u) where U : T; } public class Foo<t> : IFoo<t> { void IFoo<t>.Hello<s> (T t, S s) { } }

The same applies when overriding an abstract or virtual method.

However, if you implement the interface method implicitly, you must repeat the constraints - and they may not conflict with the ones from the declaration in the interface:

public interface IFoo<t> { void Hello<u> (T t, U u) where U : T; } public class Foo<t> : IFoo<t> { public void Hello<s> (T t, S s) where S : T { } }
Posted by martin at August 25, 2004 4:27 pm.

August 17, 2004 5:53 pm (permalink)

Generic observations

Today, I tracked a really bad boy down which caused very weird failures of gmcs compiled assemblies on the MS runtime. So, while looking at the following example, I made some interesting obervations:

interface IFoo<t> { void Test<u> (T t, U u); } class Foo<t> : IFoo<t> { void IFoo<t>.Test<u> (T t, U u) { } } class Bar<t> : IFoo<int> { void IFoo<int>.Test<u> (int a, U u) { } }
  • The most important thing is that the entries in the GenericParam table must be sorted according to the owner field.

  • In IL, we generate
    .class interface private abstract auto ansi 'IFoo`1'<T>
    

    and

    .class private auto ansi beforefieldinit 'Foo`1'<T>
           extends [mscorlib]System.Object
           implements class 'IFoo`1'<!T>
    
  • However, for the explicit interface implementation, we generate
    .method private hidebysig newslot virtual final instance void  'IFoo<T>.Test'<u>(!T t, !!U u) cil managed
    

    Note that this does not include the generic arity like in the class name.

  • To make things really confusing, we do use the arity in the .override:
    .override  method instance void class 'IFoo`1'<!T>::Test<[1]>(!0,!!U)
    
Posted by martin at August 17, 2004 5:53 pm.

August 16, 2004 11:43 am (permalink)

C5

Last week, Peter Sestoft and Niels Jørgen Kokholm released C5, a comprehensive library of generic collection classes for C# 2.0.

This is really good news for me, since it gives me plenty of test cases for GMCS - it can't compile C5 yet, but I'm really confident that I can fix the bugs really soon. This'll bring us one step further on the way to a stable, high-quality compiler for C# generics.

I imported a copy of this library into CVS (mcs/class/Mono.C5), but it isn't included in the build yet.

Posted by martin at August 16, 2004 11:43 am.

August 13, 2004 2:30 pm (permalink)

All good things ... must come to an end - part II

So this is finally it, the very last picture of my old car:

Now it's finally time to say goodbye ....

Posted by martin at August 13, 2004 2:30 pm.

August 09, 2004 06:16 am (permalink)

All good things ... must come to an end

This is it, the sad and final end of my old car.

Just took a very last picture of it and removed the license plates. Now it's time to say goodbye and look forward to getting a new car ....

Posted by martin at August 09, 2004 06:16 am.