[Issue 4086] New: Standard struct constructor for the heap

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Apr 12 23:56:43 PDT 2010


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

           Summary: Standard struct constructor for the heap
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-04-12 23:56:41 PDT ---
D allows to initialize a struct on the stack with a default syntax:

struct Node {
    int data;
    Node* next;
}
void main() {
    Node n1 = Node(10); // OK
    Node n2 = Node(10, null); // OK
}


So I propose to introduce that same handy automatic syntax for heap allocation
too:

struct Node {
    int data;
    Node* next;
}
void main() {
    Node* n1 = new Node(10); // OK
    Node* n2 = new Node(10, null); // OK
}


This feature doesn't increase the complexity of the language for the
programmer, it lowers the complexity because there is one less special case to
remember (even if the compiler can become a bit more complex).


To keep things tidy, such default costructor must be absent if any other
costructor is defined in the struct:

struct Foo {
    int data;
    Foo* next;
    this(int d, Foo* p) {
        data = d;
        next = p;
    }
}
void main() {
    Foo* f = new Foo(10); // Error, standard constructor is absent
}

-- 
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