'new' class method
KennyTM~
kennytm at gmail.com
Fri Oct 24 10:04:05 PDT 2008
Benji Smith wrote:
> Bill Baxter wrote:
>> On Fri, Oct 24, 2008 at 3:01 PM, Benji Smith
>> <dlanguage at benjismith.net> wrote:
>>> Why not just introduce "heap" and "stack" keywords, and say it directly?
>>
>> Did you see my subsequent proposal? I'm actually quite fond of it.
>> But no one has responded to it. :-( [Bill cry's himself a silent
>> tear.]
>
> You mean the one where you suggested this?
>
> auto a = Class(); // heap (default)
> auto a = Struct(); // stack (default)
>
> auto a = Class.new(stack)(); // stack
> auto a = Struct.new(stack)(); // stack
>
> auto a = Class.new(heap)(); // heap
> auto a = Struct.new(heap)(); // heap
>
> void *addr;
> auto a = Class.new(addr)(); // placement
> auto a = Struct.new(addr)(); // placement
>
I strongly *oppose* using stack and heap as keywords because there are
also (partly irrelevant) data structures called stack and heap, even
though according to the guideline these class/structs should be named
Stack and Heap.
And I think we can include the meaning that “scope” attribute always
allocate on stack (Currently 4 conditions need to be met for so), but
I'm afraid this may break something (otherwise the other 3 conditions
won't be there?). Maybe some experts can comment on this?
(Ref: http://www.digitalmars.com/d/2.0/memory.html#stackclass)
(Oh, BTW, please fix bug #1521 for clarification.)
And I suggest keeping the “new” syntax for heap ("return a pointer")
allocation. So,
auto c = Class(...); // default on heap
auto s = Struct(...); // default on stack (compatible with current syntax)
scope c = Class(...); // on stack
scope s = Struct(...); // on stack (compatible with current syntax)
auto c = new Class(...); // on heap (compatible with current syntax)
auto s = new Struct(...); // on heap (compatible with current syntax)
auto c = new(...) Class(...); // placement (compatible with current syntax)
auto s = new(...) Struct(...); // placement (compatible with current
syntax?)
/* if you want symmetry, do it like this:
new c = Class(...);
new(...) c = Class(...);
kinda strange for me. */
These will cause minimum change while eliminating the “new” for most
cases. This won't free the “new” keyword though.
> Yeah! I noticed it. And I like it. In fact, I thought to myself "that's
> almost the same as my idea". Generally, I'm not a fan of the consecutive
> sequence of parentheses (especially that templates already make for
> double parens; I can't imagine I'd want triple parens for a templatized
> consttructor call).
>
> For non-template chained calls, I'm not quite sure which I prefer:
>
> auto a = Foo.new(heap)(Bar.new(stack)());
>
> auto a = on(heap) Foo(on(stack) Bar());
>
>> New is already a keyword meaning allocation/construction in D, Java,
>> C++ and maybe C#? So anyone familiar with those will know right away
>> that "new" probably has something to do with construction. So the
>> question is why wouldn't you use "new"?
>
> Well, in Java and C#, objects can *only* be allocated on the heap, and
> structs can *only* be allocated on the stack (unless they're members of
> an object, or if they're in an array).
>
> To me, "new" is a great word for "allocate, and then invoke the
> constructor", but it doesn't have anything to do with *HEAP* allocation,
> per se.
>
> And then I thought to myself "for chrissakes, if you want the heap or
> the stack, why not ask for it by name?"
>
> --benji
More information about the Digitalmars-d
mailing list