auto storage class - infer or RAII?

Georg Wrede georg.wrede at nospam.org
Sun Nov 12 11:10:10 PST 2006


Walter Bright wrote:
> Sean Kelly wrote:
> 
>> Walter Bright wrote:
>>
>>> The auto storage class currently is a little fuzzy in meaning, it can 
>>> mean "infer the type" and/or "destruct at end of scope".
>>
>>
>> As Don explained to me, 'auto' is a storage class in D and in C along 
>> with 'const' and 'static', so type inference doesn't occur because 
>> 'auto' is present so much as because a type is omitted.  The presence 
>> of 'auto' merely serves to indicate that the statement is a 
>> declaration (since 'auto' is the default storage class and therefore 
>> otherwise optional).  Type inference occurs with the other storage 
>> classes as well.  I think this is an important distinction because it 
>> seems to be a common misconception that 'auto' means 'infer the type 
>> of this expression' and that a specific label is necessary for this 
>> feature.
> 
> 
> True. Consider that type inference works in these cases:
> 
>     static a = 3;    // a is an int
>     const b = '3';    // b is a char
> 
> So auto doesn't actually ever mean "infer the type", it's just needed 
> because one of the other storage class keywords isn't there.

I think it's okay to "have a word" for "infer the type". Think about it 
like this: through years C folks have used for(int i=0; i<10; ++i) as 
the default loop construct, never suspecting how shallow their 
understanding of it really is. But only people with a deeper 
understanding could write something like for(Node p=list; p; p=p.next) 
out of their own head.

Similarly, having "a word" for automatic type inference is okay, and it 
won't become any more a hindrance on your way to become a top programmer 
than the similar misconception about for. Once you've understood the 
language enough, you'll figure out that it's simply about not specifying 
a type and just needing a placeholder in the syntax if no other storage 
attribute happens to be needed.

So, it should be no biggie.

---

That would be, IMHO, a better solution than the current, where people 
instead run in circles chasing wild geese, and even highly admired D 
gurus like Sean have to ask Don about what the hell is going on here. (I 
know I should have asked Don!)

THAT IS UNACCEPTABLE. It is imperative that the language be designed so 
that such massive confusion simply does not arise.

(I know this post may seem self-contradictory at first reading. :-) )



More information about the Digitalmars-d mailing list