[Mono-devel-list] Mono .net 2.0 preview and generics

Ben Martin benmartin at fourthplanet.net
Thu May 13 15:29:23 EDT 2004


Gert Kello wrote:

>
> the method like
> public void Something(ref int Param)
>
> must be called like
>
> int aParam = 0;
> Something(ref aParam);
>
>> [prw at desdemona generics]$ gmcs Gsort.cs
>> Mono C# Compiler 0.91.0.0 for Generics
>> Gsort.cs(70) error CS1502: The best overloaded match for method 'void 
>> Polysort.swap (T, T)' has some invalid arguments
>> Gsort.cs(70) error CS1503: Argument 0: Cannot convert from 'ref T' to 
>> 'T'
>> Gsort.cs(103) error CS1502: The best overloaded match for method 
>> 'void Polysort.swap (T, T)' has some invalid arguments
>> Gsort.cs(103) error CS1503: Argument 0: Cannot convert from 'ref T' 
>> to 'T'
>> Compilation failed: 4 error(s), 0 warnings
>>
>


Actually his code does that properly as far as I can see...

This appears to be a bug in gmcs... Consider the following code which 
compiles but then chokes when run (of course...). It SHOULD NOT compile 
as is... And if you change it to USE the ref modifiers like it supposed 
to, it doesn't work... (Incidentally I tried it with non-generic classes 
and it works as expected, so it is evidently a problem only with the 
generics part). (I am using beta1, I am assuming this has not been fixed 
already; I didn't see anything about it on bugzilla).

Ben Martin


// Code to test the ref modifier for generic functions

class RefTest {
  public void swap <T> (ref T i, ref T j) {
    T temp;
    temp = i;
    i = j;
    j = temp;
    return;
  }
}

class Stuff <T> {

  public Stuff(T a) {
    data = a;
  }

  public void Print() {
    System.Console.WriteLine("data = {0}", data);
    return;
  }

  private T data;
}

class Driver {

  public static void Main()
  {
    RefTest refTest = new RefTest();
   
    Stuff<int> x = new Stuff<int>(4);
    Stuff<int> y = new Stuff<int>(5);
    refTest.swap<Stuff<int>>(x, y);  // ********* Line 35 **********
    // Above should NOT work, below SHOULD
    // refTest.swap<Stuff<int>>(ref x, ref y);
    x.Print();
    y.Print();
  }

}



More information about the Mono-devel-list mailing list