'new' class method

Benji Smith dlanguage at benjismith.net
Fri Oct 24 06:10:23 PDT 2008


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

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