[Mono-list] public parameterless constraint abstract generic method bug in mono 1.2?

Ben Joldersma ben at joldersma.org
Sun Nov 19 01:42:14 EST 2006


Hello all,

I wrote a few months ago about a problem I was having with the new()
constraint on some generic abstract methods I was working with in a
project.  Today, I was trying to update my project to mono 1.2, and I
noticed that the code no longer compiles.  I distilled the issue down to a
sample code file I'm including.

It looks like the issue was fixed which prevented a generic method in a
derived class from overriding an abstract or virtual method in the base
class, in the cases where the base class defined a public parameterless
constraint (I don't know if this applies to other kinds of constraints.)
However, now, I am unable to call a second generic method that has the new()
constraint inside the derived method.  It complains that the type T must
have a public parameterless constructor in order to use it in the function
invocation.

Does this make sense?  Is there a workaround?  I suppose I could fudge
things for now by implementing interfaces in the abstract and derived
classes, but I'd like this to just work.  Any feedback appreciated.

following is the repro file.

cheers,

--ben

//BEGIN_FILE
using System;

namespace testConstraintsVirtuals
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }

    abstract class Base
    {
        public abstract void method<T>() where T : new();
    }

    class Derived : Base
    {
        // the first method signature is the ideal one.  this one compiles
with ms csc.  it's what you'd expect to do.
        // in mono 1.1.17, you could remove the override, and specify
constraints, and it would compile, now no longer.
        // gives error: The type `T' must have a public parameterless
constructor in order to use it as parameter `T' in the generic type or
method `testConstraintsVirtuals.Derived.method2<T>()'(CS0310)
        public override void method<T>()

        // public override void method<T>() where T : new()
        // gives error: Cannot specify constraints for overrides or explicit
interface implementation methods

        // public void method<T>() where T : new()
        // gives error: testConstraintsVirtuals.Derived.method<T>()' hides
inherited abstract member

        // new public void method<T>() where T : new()
        // gives error: testConstraintsVirtuals.Derived.method<T>()' hides
inherited abstract member
        {
            method2<T>();
        }

        public void method2<T>() where T : new()
        {

        }
    }
}
//END_FILE
-- 
ben joldersma
http://ben.creationsnetwork.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20061118/e7ffac3a/attachment.html 


More information about the Mono-list mailing list