'new' class method

Benji Smith dlanguage at benjismith.net
Thu Oct 23 23:01:04 PDT 2008


Bill Baxter wrote:
> How about this as a start:
> 
> // Default cases
> auto a = SomeStruct(arg);  // struct on the stack
> auto a = SomeClass(arg);  // class on the heap
> 
> // Anti-default cases
> auto a = SomeStruct.new(arg);  // struct on the heap
> scope a = SomeClass(arg);  // class on the stack
> 
> // Placement cases
> auto a = SomeStruct.new[placement_arg](arg); // placement struct
> auto a = SomeClass.new[placement_arg](arg); // placement class

I don't particularly care about getting rid of the "new" keyword. In 
fact, I especially like the part about getting rid of this little ugly:

    // uuuuuugly
    auto x = (new MyClass()).whatever();

    // holy crap. so much better.
    auto x = MyClass().whatever();

But I think it's weird that people want to retain "new" to indicate heap 
construction and use the word "scope" to indicate stack construction. 
Except for structs, which would have different rules.

Ugh.

Why not just introduce "heap" and "stack" keywords, and say it directly?

    stack auto x = MyClass();
    heap auto y = MyStruct();

...or, since "heap" and "stack" are good user keywords, maybe introduce 
an "on" keyword. Maybe something like this:

    auto x = on(stack) MyClass();
    auto y = on(heap) MyStruct();

To me, there's nothing about the "new" keyword that implies heap 
construction. "Scope" is a little bit better, because I know the memory 
will be cleaned up at the end of the scope, but that doesn't 
*necessarily* mean stack allocation, per se.

--benji



More information about the Digitalmars-d mailing list