auto storage class - infer or RAII?

Kristian Kilpi kjkilpi at gmail.com
Sun Nov 12 08:38:17 PST 2006


On Sat, 11 Nov 2006 20:48:00 +0200, Walter Bright  
<newshound at digitalmars.com> 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.

I, too, think that we should have new keywords for type inference and RAII.

I like 'var' for type inference. For RAII, I guess 'scope' is ok, even if  
it's a bit 'funny'; 'scoped' would be better. 'local' would be nice too:  
it's adjective.

For example (I use 'scoped' here):

   var a = 1;
   var int b = 2;

   var c = new Class;
   scoped d = new Class;      //RAII
   scoped var e = new Class;  //RAII


What if a RAII keyword could be used with 'new' too?

   var a = new scoped Class;  //RAII
   var b = scoped Class;      //RAII


Well, I think 'auto' would be useful with 'RAII classes' and 'RAII member  
variables', if they are implemented in the future. (If a better word  
cannot be invented, of course.) E.g.

   class Foo {
     auto Bar b;  //'b' is automatically destroyed with 'Foo'
     }



More information about the Digitalmars-d mailing list