'new' class method

Bill Baxter wbaxter at gmail.com
Fri Oct 24 00:58:23 PDT 2008


On Fri, Oct 24, 2008 at 3:01 PM, Benji Smith <dlanguage at benjismith.net> wrote:
> 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?

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

>   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();

That's kinda cute.    "in" would almost work there.    Too bad we
don't usually say "put it in the stack" in CS.  :-)

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

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

--bb



More information about the Digitalmars-d mailing list