'new' class method

Bill Baxter wbaxter at gmail.com
Thu Oct 23 13:31:44 PDT 2008


On Fri, Oct 24, 2008 at 5:07 AM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> "Bill Baxter" wrote
>> To me this makes a lot of sense:
>>
>> auto a = SomeStruct();  // a is a struct on the stack
>> auto b = SomeClass();  // b is a class on the heap
>>
>> It kills static opCall for classes, but who cares.  I'm often tempted
>> to write static opCalls for classes like this anyway:
>>    static SomeClass opCall() {  return new SomeClass(); }
>>
>> Just to get the uniformity of construction syntax.
>
> It also cleans up this ugliness:
>
> auto data = (new FileInput(filename)).readAll();
>
> Which then becomes:
>
> auto data = FileInput(filename).readAll();

But, ok, this isn't Python so we have to have a story for placement new.
I believe use of placement new is pretty rare.  (I use it rarely,
anyway. :-) )   If so it should be ok if the syntax is a little
clunky.

Also heap construction of structs.

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

Not sure about those brackets.  Kind of odd-ball, but placement new is
kind of odd-ball anyway.  And I think we agreed that such syntax was
workable for templates, if not pleasant for the programmer.  But new
expressions are rarely part of mondo compound expressions, and there's
the keyword "new" to clue you in, so I think it should be acceptable
in this case.

--bb



More information about the Digitalmars-d mailing list