On 01/16/2010 01:46 AM, Leandro Lucarella wrote:
Ali Çehreli, el 15 de enero a las 16:01 me escribiste:
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? :)
I don't think so. auto means in D the same that in C/C++, the difference
is that D do type inference when a *storage class* is given. const,
static, immutable, shared are other storage classs, so when you used
them, you can infer the type too (if no type is given).
You can do const auto c = 1; (I think), but I can't do static auto c = 1;
(I think too). You can omit auto when declaring automatic variables if you
specify the type (seen the other way :), because it defaults to auto. And
you can omit the type if you use a storage class, because it defaults
to the infered type.
Makes sense, but static auto totally works.
I think auto just means inferred type.