@disable

Leandro Lucarella llucax at gmail.com
Fri Jan 15 16:46:14 PST 2010


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.

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
¿Qué será lo que hace que una brújula siempre marque el norte?
- Ser aguja, nada más, y cumplir su misión.
	-- Ricardo Vaporeso



More information about the Digitalmars-d mailing list