Thread overview
[Issue 1735] New: classinfo.create returns null if no default constructor
Dec 16, 2007
d-bugmail
Dec 17, 2007
d-bugmail
Feb 16, 2008
d-bugmail
December 16, 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@digitalmars.com
        ReportedBy: wbaxter@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.


-- 

December 17, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1735





------- Comment #1 from dhasenan@gmail.com  2007-12-16 21:36 -------
This is an interesting situation. Related to #1712 -- you can't fake the functionality because of interfaces. Otherwise, it's a pretty dangerous situation to create an instance of a class without calling the constructor.

I'd say the default action should be throwing an exception if there is no default constructor. It would be convenient to have an official, endorsed method for getting an object of a given type without calling the constructor, though.


-- 

February 16, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1735





------- Comment #2 from bugzilla@digitalmars.com  2008-02-16 12:44 -------
At the moment I'm just going to document that it returns null if there is no default constructor. It's a bad idea to create an object anyway if there are constructors but no default one.


-- 

October 11, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=1735


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|bugzilla@digitalmars.com    |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1735


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |INVALID


--- Comment #3 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-09-26 11:47:00 PDT ---
The behavior is as intended. For lower-level object constructions mechanisms, emplace() is recommended.

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