@disable
Ary Borenszweig
ary at esperanto.org.ar
Sat Jan 16 06:07:01 PST 2010
Pelle Månsson wrote:
> 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.
Why not read the specification?
http://www.digitalmars.com/d/2.0/attribute.html#auto
"The auto attribute is used when there are no other attributes and type
inference is desired."
So basically type inference is always done except when you specify the type.
More information about the Digitalmars-d
mailing list