auto storage class - infer or RAII?

Sean Kelly sean at f4.ca
Sat Nov 11 19:47:35 PST 2006


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.

 > 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.

This is how I currently understand the syntax to work, and once 
understood it seems fairly clear.  However, I don't like overloading the 
meaning of keywords if it can be avoided, particularly when both 
meanings apply to the same keyword used in the same context.  Also, I'm 
not sure I like specifying scoped destruction by modifying the reference 
variable instead of the data.  First, this confuses the idea of 
reference types in that the references themselves are scoped.  Second, 
this doesn't allow for compilers to create referenced data on the stack 
as a QOI feature because the auto-destruction quality is attached to the 
reference, not the data.  For example:

     class MyClass
     {
         this( char[] s )
         {
             label = s;
         }

         ~this()
         {
             printf( "dtor: %.*s\n", label );
         }

         char[] label;
     }

     void main()
     {
         {
             auto MyClass c = new MyClass( "a" );
             c = new MyClass( "b" );
         }
         printf( "done\n" );
     }

prints:

     dtor: b
     done
     dtor: a

If the current syntax is to be preserved, I would suggest that auto 
references at least not be reassignable to prevent this behavior.

> In the future, I'd like the following to work:
> 
> 5) auto c = Class();
> 
> which would mean type inference *and* RAII.

I like this.  It is consistent with the declaration of other types in D 
('new' means dynamic, otherwise scoped), and it associates scoped 
destruction with the data rather than with the reference which is 
exactly as it should be.  In fact, if I had any request related to this 
syntax it would be that the ctor aspect eventually be available for 
concrete types as well.  Not an urgent request as it would be a new 
feature, but I thought I'd mention it anyway.  Also, I don't see any 
reason to tie stack vs. heap construction to this new syntax.  It could 
remain a QOI feature for compilers as far as I'm concerned.

By the way, I assume this change would do away with:

     auto class MyClass {}

as a valid declaration syntax as well?  Just wanted to be clear on this.


Sean



More information about the Digitalmars-d mailing list