Jesse Phillips wrote:
>> Even the keyword 'auto' is C's defunct 'auto' in C++0x (and I strongly
>> belive also in D); just because "automatic variable" and "automatically
>> typed" both start with "auto". :)
>>
>> Ali
>
> If I'm not mistaken (surely I am), D's auto keyword is the same as C's.

C's auto is about lifetimes (storage duration). 'auto' makes function local variables and parameters automatically terminated upon exit from functions.

Since that's the default in C (and C++), 'auto' is redundant there. That's why the keyword has been reused by C++0x to mean "infer the type".

> But D has type inference so we just state the storage class and not the type.
>
> http://www.digitalmars.com/d/2.0/declaration.html#AutoDeclaration

It is news to me that the following works without 'auto':

struct S
{
    int i;

    this(int i)
    {
        this.i = i;
    }
}

void main()
{
    const c = S(42);   // <-- no 'auto' needed
}

Now I realize that 'auto' is for when we want type inference for mutable variables because this doesn't work:

    c = S(42);   // <-- compiler ERROR

So we have to use a keyword:

    auto c = S(42);
    ++c.i;

If I understand it correctly, 'auto' serves as the nonexistent 'mutable' keyword in this case.

I think to be consistent, I will continue using 'auto' even for when a storage class is specified:

    const auto c = S(42);  // works too

For me, that gives 'auto' a single meaning: "the inferred type".

Do I get it right? :)

Ali

Reply via email to