Auto syntax revisited

Ivan Senji ivan.senji_REMOVE_ at _THIS__gmail.com
Tue Feb 21 08:29:54 PST 2006


Fredrik Olsson wrote:
> Sean Kelly skrev:
> 
>> 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 see your point, especially with the object later being replaced. Maybe 
> I am trying to shoot to far By arguing for not focusing on "stack-based" 
> but rather on "destroyed when exiting scope".
> 
> 
>> I think the distinction is important because foo can be reassigned to 
>> a non-local object.  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.
>>
> But still I find it could be usefully, especially in a case like this 
> when using a function as a to create the objects. But to solve this I 
> think the distinction should be associated with the assignment, not the 
> variable, nor the object.
> 
> Foo Bar() { return new Foo(); }
> auto foo = local Bar();
> auto baz = local new Baz();
> 
> My point still being; that we should not be so focused on how the 
> technical bits like how and where the object is allocated (on stack or 
> heap), but how it should be handled (local to scope or not). 

I think I agree (although not 100% certain yet)
Think it would be cool if local men't destruct at end of scope no mater 
is the actual instance created in this function or not.

Stack objects could still be created something like:
auto foo = Foo(); or maybe
auto baz = stack Foo(); or something else

I somehow feel these features might complicate the language a lot.



More information about the Digitalmars-d mailing list