Auto syntax revisited

Fredrik Olsson peylow at gmail.com
Mon Feb 20 12:23:26 PST 2006


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();

Why? First of auto is only used for the implicit type, so no confusion. 
  The keyword local in itself describes pretty to the point what is 
supposed to happen with the variable. And nothing is said about the 
stack, so we are future proof if in the future we would like to also have:

local auto foo = Bar();

Where Bar(); is a function returning an Object, but we still want the 
object to be destructed when going out of this scope. The implementation 
is quite different as the Object would need to be on heap, but the 
syntax is the same. So local would indicate what should be done (destroy 
when out of scope), not how it should be done (allocate on stack or 
whatever).

Even this could be possible, without syntax changes:
{
   local Foo bar;
   // some code
   Baz(bar); // Jupp Baz have a inout parameter returning an object.
} // And bar is still destroyed if set to something here...


regards
// Fredrik Olsson



More information about the Digitalmars-d mailing list