Suggestion: shortcut for 'new X'

Kristian kjkilpi at gmail.com
Fri Aug 25 01:34:53 PDT 2006


On Fri, 25 Aug 2006 00:01:46 +0300, Sean Kelly <sean at f4.ca> wrote:
> Chris Nicholson-Sauls wrote:
>> Kristian wrote:
>>> How about this:
>>>
>>>     Obj obj1 = new Obj;
>>>     Obj obj2 = new Obj(10);
>>>
>>> could be reduced to:
>>>
>>>     Obj obj1 = new;
>>>     Obj obj2 = new(10);
>>>
>>> This way only one class name would be required per variable. (That is,  
>>> one  at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid of  
>>> course.)  Redundance would be reduced.
>>>
>>> I know, it looked quite 'odd' for me too at first, but now it looks  
>>> very  natural, and even clearer than the longer way.
>>>
>>>
>>> I'm not sure if using of this syntax should be possible outside the   
>>> variable declarations. Later in a code "obj1 = new;" is less  
>>> informative  than "obj1 = new Obj;". However, someone may like it  
>>> too... it's up to a  programmer how to code (which is not always a  
>>> good thing(tm) though).
>>  Would this pose any ambiguities with custom class allocators?
>
> I was wondering the same thing, but I think it could be resolved via  
> look-ahead.  That said, I'm not sure I like the syntax as it seems a bit  
> confusing.  Is it really so hard to specify the type name?

Never underestimate the power of the dark laziness! But seriously... One  
could also ask why 'this' is used in constructors/destructors instead of  
the class name. I was very pleased to find out that D does that.

In Qt, for instance, you normally use pointers to objects. So, every time  
I wrote "X ... X" (e.g. "QCanvasPolygonalItem *item = new  
QCanvasPolygonalItem;") I was thinking that there must be a better way to  
do that.


I started to use a new, bigger monitor a while ago. There was a negative  
side to it, however, paying money. And I thought I don't need it, not  
really, the old one has served me fine in the past... Well, now I wouldn't  
switch back to the old one.



More information about the Digitalmars-d mailing list