Eliminate "new" for class object creation?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Oct 19 15:58:51 PDT 2009


Denis Koroskin wrote:
> On Tue, 20 Oct 2009 02:38:13 +0400, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> I'm having a hard time justifying that you use
>>
>> new X(args)
>>
>> to create a class object, and
>>
>> X(args)
>>
>> to create a struct object. I wrote this:
>>
>> ============
>> The syntactic  difference between  the expression creating  a @struct@
>> object---Test(@\meta{args}@)@---and the  expression creating a @class@
>> object---\cc{new Test(}\meta{args}@)@---may be  jarring at first. \dee
>> could have dropped the @new@  keyword entirely, but that @new@ reminds
>> the programmer that an object allocation (i.e., nontrivial work) takes
>> place.
>> ===============
>>
>> I'm unhappy about that explanation because the distinction is indeed 
>> very weak. The constructor of a struct could also do unbounded amounts 
>> of work, so what gives?
>>
>> I hereby suggest we get rid of new for class object creation. What do 
>> you guys think?
>>
>>
>> Andrei
> 
> This is tricky. How would you explain that the following
> 
> Test test;
> 
> initializes a variable if it is struct, but doesn't initialize if it's a 
> class?
> (*hint* non-nullable and explicit initialization *hint*)
> 
> test.foo(); // why is my newly-created class object segfaulting but 
> struct doesn't?

That's a good point, thanks.

Andrei



More information about the Digitalmars-d mailing list