auto storage class - infer or RAII?

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Nov 11 19:34:30 PST 2006


"Walter Bright" <newshound at digitalmars.com> wrote in message 
news:ej55ss$1g6m$1 at digitaldaemon.com...
> 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();

No effing way, period.  That is unclear as sin.  That's like copy 
constructors in C++.

My preference would be a separate keyword which would take the place of the 
type entirely.  It would be treated by the compiler as a type, albeit one 
which is unknown until the initializer has been semantic'ed.  For example, 
let's call it 'var':

var x = 5;
var y = "hello";

typeof(x) yields int and typeof(y) yields char[].

This has the nice property of being forward-compatible with more interesting 
things down the road.  How about ATI specialization?

var[] x = someFunc();

where x must be an array of any type, which is automatically inferred.  It's 
probably not something that would be that necessary, but it's nice that 
something like this is possible.

Failing a change for ATI, I'll stick with my previously-proposed change for 
RAII: to use 'scope' as a storage class for a declaration to indicate that 
it's RAII.

scope A a = new A();
scope a2 = new A(); // use ATI since 'scope' functions as a storage class

That, or both:

scope var a2 = new A(); // RAII and ATI

In any case, I think the replies to this thread (and, well, just about every 
other thread that has EVER been started about this topic) show that you are 
in a very, very small minority when it comes to ideas about how ATI and RAII 
should be represented by the syntax.  Please do not pull a "just because C++ 
uses auto for ATI, D should too".  D is not going to become a unique 
language by taking (mis)features from C++ just to appease the nitpicky C++ 
crowd (which, by now, you should know full well can NEVER be pleased). 





More information about the Digitalmars-d mailing list