Proposal: new variable definition operator

Daniel Keep daniel.keep.lists at gmail.com
Sat Apr 21 06:09:24 PDT 2007



jovo wrote:
> Daniel Keep Wrote:
>> Just want to nitpick one thing: you seem to think that "auto" is the
>> type inference keyword.  It isn't.  In D, a declaration is made up of
>> four parts: the storage class, the type, the identifier and the initialiser.
>>
>> Obviously the identifier always needs to be there.  If omitted, the
>> initialiser is just type.init.
>>
>> Storage class and type are the interesting ones.  In C, you could always
>> omit the storage class if it was just a regular variable, but always had
>> to specify the type.  D allows you to omit the type if and only if you
>> specify the storage class.  Guess what the default storage class is?
>>
>> auto.
>>
>> Type inference in D isn't "type inference triggered by keyword", it's
>> "type inference by omission."  "auto" is only required so that the D
>> grammar can tell it's supposed to be an declaration.
>>
> 
> What is a meaning of auto as storage class specifier in D? 
> In C it signifyes an automatic variable (hence auto) with clear
> definition.
> 
> C++ faces similar problem introducing type inference via auto.
> They will clear auto from list of storage class specifiers.
> Just reusing old keyword with new meaning.
> 
> jovo

The auto storage class simply means that it's a "normal" variable: it's
not constant, not final, not static and not scope-destroyed.  If it's at
module-level, it's a global variable at a fixed memory location.  If
it's in a function, it's allocated on the stack.  If it's in a struct or
class, it's allocated as part of the containing type.

AFAIK, D's auto is the same as C's auto.

Just to reiterate: auto has *nothing* to do with type inference.  It's
just that it happens to be the default storage class, and specifying the
storage class allows you to omit the type.  Any storage class can be
used to trigger type inference.

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list