Ref local variables?

Nick Sabalausky a at a.a
Wed Jan 11 14:53:14 PST 2012


"Nick Sabalausky" <a at a.a> wrote in message 
news:jej186$78q$1 at digitalmars.com...
> "Ben Davis" <entheh at cantab.net> wrote in message 
> news:jeinah$2pnj$1 at digitalmars.com...
>> Hi,
>>
>> Please excuse the cross-post with D.learn. People have been helpful there 
>> with workarounds, but I'm bringing it here in the hope that we can 
>> discuss a language enhancement.
>>
>> So - could support for 'ref' local variables be added, or is there a 
>> reason not to? I want to write something like:
>>
>> MapTile[] map;    // It's a struct
>>
>> ref MapTile tile=map[y*w+x];
>> tile.id=something;
>> tile.isWall=true;
>> someFunctionThatTakesRefMapTile(tile);
>>
>
> I *really* want the ability to do that sort of thing, although I think it 
> should be an alias rather than a ref variable:
>
> MapTile[] map;    // It's a struct
>
> alias map[y*w+x] tile;
> tile.id=something;
> tile.isWall=true;
> someFunctionThatTakesRefMapTile(tile);
>

I'm wrong. Using alias would have totally different semantics from your 
example. We really should have both:

ref MapTile tileRef=map[y*w+x];
alias map[y*w+x] tileAlias;

assert(&tileAlias == &tileRef);
x++; // tileAlias now points to the next element, tileRef is unchanged
assert(&tileAlias == &tileRef + 1);





More information about the Digitalmars-d mailing list