[nem-bug] [Nemerle 0000638]: Co/contravariance for generic interfaces / delegates

feedback at nemerle.org feedback at nemerle.org
Wed Jul 26 23:57:50 CEST 2006


A NOTE has been added to this issue.
======================================================================
<http://nemerle.org/bugs/view.php?id=638> 
======================================================================
Reported By:                steffen
Assigned To:                
======================================================================
Project:                    Nemerle
Issue ID:                   638
Category:                   Compiler
Reproducibility:            always
Severity:                   feature
Priority:                   normal
Status:                     confirmed
======================================================================
Date Submitted:             03-25-2006 19:55 CET
Last Modified:              07-26-2006 23:57 CEST
======================================================================
Summary:                    Co/contravariance for generic interfaces / delegates
Description: 
It would be nice to have co/contravariance support for generic parameters
of interfaces and delegates.

I.e. you should be able to do something like this:

interface IEnumerator[[covariant] T] {
  Current : T { get; }
  MoveNext() : bool;
}

interface IComparer[[contravariant] T] {
  Compare(x : T, y : T);
}

delegate EventHandler[[contravariant] T](arg : T) : void;

(The examples are from the CLI Spec II 9.5)

Btw.: It seems tha ncc doesn't support attribute for type parameter, e.g.

public class attr : System.Attribute { }

public interface X[[attr] 'a] { }

fails to compile.
======================================================================

----------------------------------------------------------------------
 malekith - 03-25-06 20:44 
----------------------------------------------------------------------
We thought about this. I guess even SRE allows this, but we were affraid
mono SRE would just crash on it (and even if SRE supported this, the
runtime is probably not tested). Of course this is not a justficiation to
skip this feature :-)

----------------------------------------------------------------------
 nazgul - 05-03-06 18:29 
----------------------------------------------------------------------
We still need to invent the syntax ;)

I vote for  interface IList [T+] { }   and interface IFunc [F-,T+] { .. }

This feature seems to be not that hard to do, assuming that .NET runtimes
will actually handle it. Negative tests (rejecting incorrect annotations)
also looks doable. Maybe we should promote this feature to our
OpenProjects page, so it can get more attention?


----------------------------------------------------------------------
 VladD2 - 05-08-06 22:23 
----------------------------------------------------------------------
In Scala used 
IList [+T] { } 
syntax.

And what about co/contravariance and the ValueType type arguments?

----------------------------------------------------------------------
 nazgul - 05-08-06 22:55 
----------------------------------------------------------------------
I guess our paraser will handle both +T and T+ in the same way, so we can
have both.

----------------------------------------------------------------------
 malekith - 05-09-06 07:44 
----------------------------------------------------------------------
OCaml also uses +T, -T. I think we should stick with one syntax.

----------------------------------------------------------------------
 nazgul - 07-25-06 23:54 
----------------------------------------------------------------------
I played a little bit with this - the attached patch adds support for
defining co/contravariance on interfaces and using it for them.

The main lack are all the negative checks - for defining variance on other
types than interfaces, incorrect signatures of methods in such interfaces,
etc.

Also I didn't check how this stuff works for delegates.

----------------------------------------------------------------------
 VladD2 - 07-26-06 00:35 
----------------------------------------------------------------------
> Also I didn't check how this stuff works for delegates. 

C# 2.0 support co/contra-variants for delegates.

----------------------------------------------------------------------
 nazgul - 07-26-06 23:57 
----------------------------------------------------------------------
Well, C# supports co/contravariance during its construction - you can
create Fun<string,object> delegate from string->string function. But it
does not support passing the constructed delegate under different types:

delegate O DFun <I,O> (I i);

class M {
    static string foo (object o) { return o.ToString (); }

    static void expectObjectString (DFun <object,string> x) {
       x ("");
    }
    static void expectStringString (DFun <string,string> x) {
       x ("");
    }
      
    public static void Main(string[] args) {
      DFun <object, string> del = new DFun <object,string> (foo);
      expectObjectString (del);
      expectStringString (del); // csc complains: error CS1503: Argument
'1': cannot convert from 'DFun<object,string>' to 'DFun<string,string>'
    }
}


But I just tested my changes and Nemerle gets it right! I will be
commiting in a moment :)

Issue History
Date Modified  Username       Field                    Change              
======================================================================
03-25-06 19:55 steffen        New Issue                                    
03-25-06 20:44 malekith       Note Added: 0001123                          
05-03-06 18:29 nazgul         Note Added: 0001223                          
05-03-06 18:29 nazgul         Status                   new => confirmed    
05-03-06 18:29 nazgul         Description Updated                          
05-08-06 22:23 VladD2         Note Added: 0001255                          
05-08-06 22:55 nazgul         Note Added: 0001256                          
05-09-06 07:44 malekith       Note Added: 0001257                          
07-25-06 23:52 nazgul         File Added: cocontra.patch                    
07-25-06 23:54 nazgul         Note Added: 0001377                          
07-26-06 00:35 VladD2         Note Added: 0001379                          
07-26-06 23:57 nazgul         Note Added: 0001380                          
======================================================================




More information about the bugs mailing list