[Issue 4511] Contravariance problem

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Dec 9 01:18:28 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4511


Michal Minich <michal.minich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |michal.minich at gmail.com


--- Comment #1 from Michal Minich <michal.minich at gmail.com> 2010-12-09 01:16:45 PST ---
Slightly extended example shows that the returned instance is of type derived!,
but constructor is not called.

class Base {}

class Derived : Base { this () { x = 1; }  int x; }

abstract class Abstract {
    abstract Base foo();
}

class Concrete : Abstract {
    override Derived foo() {
        return new Base;
    }
}

void main() {
    auto c = new Concrete;
    auto x = c.foo();
    writeln (typeof(x).stringof); // prints Derived (even it is result of 'new
Base')
    writeln (x.x); // prints 0 (wich means Derived's constructor is not called)
    x.x = 2;
    writeln (x.x); // prints 2
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list