Auto syntax revisited

Georg Wrede georg.wrede at nospam.org
Mon Feb 20 18:31:06 PST 2006


Sean Kelly wrote:
> Fredrik Olsson wrote:
> 
>> After some discussion on #d I though why not put my thoughts into more 
>> permanent writing.
>>
>> Keyword auto is used for two reasons; implicit type and making sure 
>> the object is destroyed when going out of scope. I suggest a new 
>> keyword for the latter: local.
>>
>> local auto foo = new Bar();
> 
> 
> If we're moving towards stack-based auto classes then I'd prefer the 
> distinction be associated with the object and not the reference.  ie.
> 
> auto foo = local Bar();
> 
> I think the distinction is important because foo can be reassigned to a 
> non-local object. 

Bull's eye! Funny that nobody thought of this before.

> Alternately, simply omitting 'new' entirely might be 
> feasible, though the meaning there is less obvious.  As I said in #d:
> 
> Foo Bar() { return new Foo(); }
> 
> auto foo = Bar();
> 
> looks like a stack-based initialization even though it's not.  But 
> perhaps it doesn't matter in this case, as nothing will break if a 
> heap-based instance is used in place of a stack-based instance, it's 
> replacing things the other direction that can cause problems.

In general, it's much harder to spot something is missing than to spot 
the wrong thing. So, I definitely vote for the keyword 'local' for stack 
storage.

Besides, that's what it means in other languages, too.

Much cleaner: new allocates on heap, local on stack.



More information about the Digitalmars-d mailing list