[Issue 1735] New: classinfo.create returns null if no default constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 16 12:51:42 PST 2007


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

           Summary: classinfo.create returns null if no default constructor
           Product: D
           Version: 1.024
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: wbaxter at gmail.com


Not sure if this is a bug or just incomplete documentation.  ClassInfo.create
is documented as: "Create instance of Object represented by 'this'."
   http://www.digitalmars.com/d/1.0/phobos/object.html

However if you try to call classinfo.create on a classinfo for a class that has
no default constructor then you get back null.

It should be possible for the standard library implementation to create an
instance in any event since it knows what members exist.  The resulting object
may not be usable, but it would facilitate the implementation of dup() methods.

For example:

class Base {
    Base dup() {
       auto ret = cast(Base) this.classinfo.create;
       ret.x = this.x;
       ret.y = this.y;
       return ret;
    }
    string toString() { return "I'm Base"; }
    int x = 6;
    double y = 8;
}

class DerivedA : Base {
    this(int px, int py) {}
    DerivedA dup() {
       auto ret = cast(DerivedA) super.dup;
       ret.w = this.w;
       return ret;
    }
    string toString() { return "I'm DerivedA"; }

    long w = 42;
} 
void main()
{
    Base b = new DerivedA(5,10);
    Base c = b.dup();
}


That will create an access violation at run time because DerivedA doesn't have
a default constructor.


-- 



More information about the Digitalmars-d-bugs mailing list