Proposal: new variable definition operator
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Sat Apr 21 09:45:45 PDT 2007
jovo wrote:
> Daniel Keep Wrote:
>> 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.
>>
>
> From the D docs (Attributes):
> "The auto attribute is used when there are no other attributes
> and type inference is desired."
Same meaning, slightly different words, to this: "If type inference is desired, and there
is no special storage class, you may specify the explicit default storage class attribute
'auto' to trigger inference."
> OK, it's simple and clear, but difficult to say:
> "auto has *nothing* to do with type inference".
But it *does* have nothing to do with it. The following two declerations are, for
example, completely identical in result:
final auto foo = 123 ;
final foo = 123 ;
The point is: I get type inference from just omitting the type in the presence of a
storage class, which is needed to make the statement I'm issuing have the grammar of a
variable decleration in the first place.
`(storage | (storage? type)) ident ('=' init)? ';'`
All of the following declerations experience type inference:
# auto alpha = 1;
# const beta = 2;
# final gamma = 3;
# invariant delta = 4;
# scope epsilon = 5;
# static zeta = 6;
The key item in the documentation is on http://digitalmars.com/d/declaration.html under
the heading 'Implicit Type Inference' and reads:
"""If a declaration starts with a StorageClass and has a NonVoidInitializer from which the
type can be inferred, the type on the declaration can be omitted."""
The docs for 'auto' on the attributes page is, frankly, a stub and should be replaced with
something a bit more clear.
>> AFAIK, D's auto is the same as C's auto.
>>
>
> Maybe it _was_ initially. But not now.
Well see this is the quirky thing. It /was/, and then it actually /wasn't/ for a while...
but now it /is/ again. The history of D has a few such loops in it. (And warts. I'm so
glad the 'instance' keyword for template expansion went bye-bye.)
> "Automatic variables are internal to a function; they come into
> existence when the function is entered, and disappear when it is
> left." (The C Programming Language, Kernighan and Ritchie)
>
Which is still essentially true for D's auto. The only difference is that 'auto' really
just means 'default' in D, and that behavior happens to be the default.
-- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list