auto storage class - infer or RAII?

Derek Parnell derek at psych.ward
Sat Nov 11 15:44:37 PST 2006


On Sat, 11 Nov 2006 10:48:00 -0800, 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". The latter only 
> has meaning for class objects, so let's look at the syntax. There are 4 
> cases:
> 
> class Class { }
> 
> 1) auto c = new Class();
> 2) auto Class c = new Class();
> 3) auto c = some_expression();
> 4) auto Class c = some_expression();
> 
> The ambiguity can be resolved by saying that if auto is used for type 
> inference, i.e. cases (1) and (3), then it does not mean RAII. If it is 
> not used for type inference, i.e. cases (2) and (4), then it does mean RAII.
> 
> In the future, I'd like the following to work:
> 
> 5) auto c = Class();
> 
> which would mean type inference *and* RAII.

Please excuse my bash and amateur crashing in here ... but Walter, for a
very intelligent person, why is it that you just don't get it?

Type inference and RAII are two distinct, unrelated and orthogonal
concepts. Using one keyword in subtly different syntax permutations to
indicate both concepts is just plain stupid.

In each of the suggested permutations above, the code reader and writer
will constantly be thinking "now what's the precise syntax again?"  This
reminds me of the exact criticism that you have leveled against some C++
constructs, and some syntax changes to D suggested by your supporters.
Namely that the subtle differences in syntax variants is a cause of bugs
and makes writing code harder - because they are not visually distinct
enough for the average person.

Why do we want Type Inference? At least two reason pop into mind; to help
write generic code, which means less rewriting/editing of code, and to
avoid using long type names (shorter words to type). Thus a short keyword
to indicate type inference would be a better idea than using a long word.

Why do we want RAII? So we don't have to also write explicit 'destructor'
invocation code and thus reduce the amount of coding required. So again, a
short keyword rather than a long keyword would be more beneficial.

However, to use the same keyword for these two unrelated uses is an ugly
wart on your language. 

Further more 'auto' is ambiguous? Is it 'automatic'-inference,
'automatic'-RAII, 'automatic'-<some_thing_else>? 

My plea is ...

(a) Get rid of 'auto'. 
(b) Create a new keyword that is more obviously read as 'type inference'.
(c) Create a new keyword that is more obviously read as 'resource
destruction at end of scope'.
(d) Allow any combination of these new keywords on the same declaration
statement.
(e) Allow any combination of these new keywords on declarations at the
module level.

That will make reading and writing D code less costly, and increase the joy
in using your wonderful language.

-- 
Derek Parnell



More information about the Digitalmars-d mailing list