www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1735] New: classinfo.create returns null if no default constructor

reply d-bugmail puremagic.com writes:
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.


-- 
Dec 16 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1735







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.


-- 
Dec 16 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1735






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.


-- 
Feb 16 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Oct 11 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1735


Andrei Alexandrescu <andrei metalanguage.com> changed:

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



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: -------
Sep 26 2010