www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3180] New: Need delegate covariance and contravariance

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180

           Summary: Need delegate covariance and contravariance
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: spec
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: rayerd.wiz gmail.com


import std.stdio;

class A {}
class B : A {}

class X
{
    A foo() { return new A; }
}
class Y : X
{
    B foo() { return new B; }
}

class V
{
    void foo(B){}
}

void main()
{
    // Class Covariance (supported already)
    {
        X x = new X;
        Y y = new Y;
        A r = x.foo();
        A s = y.foo();
        B t = y.foo();
        writeln(r); // A
        writeln(s); // B
        writeln(t); // B
    }
    // Delegate Covariance
    {
        A delegate() f = delegate A() { return new A; }; // Of course, OK.
        writeln(f()); // A

        //A delegate() g = delegate B() { return new B; }; // Need support the
covariance
        //writeln(g()); // This should be B.
    }


    // Delegate Contravariance
    {
        V v = new V;
        void delegate(B) g = &v.foo; // Of course, OK.
        //void delegate(A) f = &v.foo; // Need suport the contravariance.
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 15 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com
           Severity|normal                      |enhancement





18:13:10 PDT ---
Your contravariance example is not valid, you cannot call foo(B) with an A.

It should be:

class V
{
  void foo(A);
}


...

V v = new v;
void delegate(B) g = &v.foo; // contravariance

Also this is somewhat of a duplicate of bug 3075.  Although you do bring up
covariance for delegates, which should be implemented at the same time as
contravariance.

Sadly, Walter has decided the prior bug is an invalid enhancement request, so
most likely nothing will come of this request either.

I think the only possible way this may be included is if someone implements it
and submits it as a patch to dmd.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 15 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180






16:55:58 PDT ---

 Your contravariance example is not valid, you cannot call foo(B) with an A.
 
 It should be:
 
 class V
 {
   void foo(A);
 }
 
 
 ...
 
 V v = new v;
 void delegate(B) g = &v.foo; // contravariance
Sorry, it is my mistake.
 Also this is somewhat of a duplicate of bug 3075.  Although you do bring up
 covariance for delegates, which should be implemented at the same time as
 contravariance.
 
 Sadly, Walter has decided the prior bug is an invalid enhancement request, so
 most likely nothing will come of this request either.
 I think the only possible way this may be included is if someone implements it
 and submits it as a patch to dmd.
I am trying to make some dmd patchs, but it is difficult yet. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 17 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha yahoo.com



10:08:31 PDT ---
*** Issue 5742 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



*** Issue 3833 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 08 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180




*** Issue 4000 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 08 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|spec                        |patch, rejects-valid
                 CC|                            |yebblies gmail.com
            Summary|Need delegate covariance    |Covariance of
                   |and contravariance          |delegates/function pointers
           Severity|enhancement                 |normal



The enhancement portion of the original report is a dupe of Issue 3075, which
has been marked INVALID by Walter.

In the comments he states that delegate covariance is supposed to work, so that
part of the report is definitely a bug.

https://github.com/D-Programming-Language/dmd/pull/96 
should fix this report for function pointers.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 08 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zan77137 nifty.com



*** Issue 4218 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla kyllingen.net



*** Issue 6524 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



02:38:23 PDT ---
Partial fix:
https://github.com/D-Programming-Language/dmd/commit/306df8eaa6f8a987f76f401a1e03d8edf1f1e2ae

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 02 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|patch                       |
             Status|RESOLVED                    |REOPENED
           Platform|Other                       |All
         Resolution|FIXED                       |
         OS/Version|Windows                     |All



Partial is partial.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au



*** Issue 3267 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Severity|normal                      |critical



Part was fixed by
https://github.com/D-Programming-Language/dmd/commit/dfb683f63ec89709b0bf2760ef3b2a249ce320eb

Raising importance as while not a regression, this fixes one. (bug 6352)

Pull for the remaining common type bugs:
https://github.com/D-Programming-Language/dmd/pull/368

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Severity|normal                      |critical



Part was fixed by
https://github.com/D-Programming-Language/dmd/commit/dfb683f63ec89709b0bf2760ef3b2a249ce320eb

Raising importance as while not a regression, this fixes one. (bug 6352)

Pull for the remaining common type bugs:
https://github.com/D-Programming-Language/dmd/pull/368


Part was fixed by
https://github.com/D-Programming-Language/dmd/commit/dfb683f63ec89709b0bf2760ef3b2a249ce320eb

Raising importance as while not a regression, this fixes one. (bug 6352)

Pull for the remaining common type bugs:
https://github.com/D-Programming-Language/dmd/pull/368

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Severity|normal                      |critical

Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED



Part was fixed by
https://github.com/D-Programming-Language/dmd/commit/dfb683f63ec89709b0bf2760ef3b2a249ce320eb

Raising importance as while not a regression, this fixes one. (bug 6352)

Pull for the remaining common type bugs:
https://github.com/D-Programming-Language/dmd/pull/368


Part was fixed by
https://github.com/D-Programming-Language/dmd/commit/dfb683f63ec89709b0bf2760ef3b2a249ce320eb

Raising importance as while not a regression, this fixes one. (bug 6352)

Pull for the remaining common type bugs:
https://github.com/D-Programming-Language/dmd/pull/368


23:28:48 PDT ---
https://github.com/D-Programming-Language/dmd/commit/77bed134d06e6314c5b65465068f554b3f2c2e8d

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180




09:04:50 PDT ---
import std.stdio;

class Base  {}
class Derived : Base {}

class X
{
    Base foo() { return new Base; }
}
class Y : X
{
    Derived foo() { return new Derived; }
}

void main()
{
    // Covariance is good
    {
        Base delegate() f = delegate Derived() { return new Derived; };
        writefln("delegate convariance is <%s>", f().toString() == "a.Derived"
? "OK" : "NG");
    }{
        static Derived fp() { return new Derived; }
        Base function() f = &fp;
        writefln("function pointer covariance is <%s>", f().toString() ==
"a.Derived" ? "OK" : "NG");
    }

    // Contravariance is BAD
    {
        auto c = new class { void foo(Base){} };

        // GOOD
        void delegate(Base) f = &c.foo;
        f(new Base);
        f(new Derived);

        // BAD
        void delegate(Derived) g = &c.foo;
        g(new Derived);
    }
}

a.d(33): Error: cannot implicitly convert expression (&c.foo) of type void
delegate(Base) to void delegate(Derived)

---

Why is "Status" "RESOLVED-FIXED"?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 17 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3180





 
 Why is "Status" "RESOLVED-FIXED"?
I assume you're asking why this report was marked as fixed when delegate covered by another report which has been rejected. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 17 2011