auto storage class - infer or RAII?

Bill Baxter wbaxter at gmail.com
Sat Nov 11 17:35:21 PST 2006


Bill Baxter wrote:
> Derek Parnell wrote:
> 
> So ATI is more common than RAII.  Maybe ATI is common enough that it 
> even warrants special syntax?  If so then I think the at-sign makes 
> sense for that. "AuTo"->"at" -- close enough for a mnemonic.  That would 
> mean "auto foo" could be compressed to "@foo".
> 
> Some examples of what it would look like:
> 
>    @ foo = some_expression();
>    @bar = other_thing();
> 
>    for (@v = start; v<end; v++) {...}
> 
>    if (@m = std.regexp.search("abcdef", "c()"))
> 
>    @dim = this.dim();
> 
>    sort(list, @(@a, at b){ return a.val-b.val; }
>      // though I think in this case it should be allowable to omit the
>      // auto's/@'s and just have sort(list, (a,b){...});

Addendum:

I personally find the implicit 'auto' in foreach to be a little jarring. 
  Every time I see

     foreach(e; elements) {}

I find myself looking around for where e is declared.   Especially since 
foreach looks so much like a variation on a for loop, but 'auto' isn't 
automatic in a for loop in D (though after I learned about foreach, I 
assumed incorrectly it would work in D's for-loop too.).

Additionally I keep getting bitten by things like
     int i;
     for (i=1; i<10; i++) {
     }
     ...
     foreach(i,elem; elements) {
        if (...) break;
     }
     writefln("last i was", i)

Which using 'foreach-is-like-for' mentality seems perfectly natural but 
causes the compiler to complain.

Anyway, @e might be short enough that there wouldn't be any need to 
special case the foreach.  Just use

     foreach(@i, at e; elements) { ... }

--bb



More information about the Digitalmars-d mailing list