Discussion on Go and D

Artur Skawina art.08.09 at gmail.com
Mon Apr 9 02:48:48 PDT 2012


On 04/09/12 02:21, Andrej Mitrovic wrote:
> On 4/9/12, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
>> and pass-by-alias
> 
> Speaking of alias, one killer feature would be to enable using alias
> for expressions. E.g.:
> 
> struct Window { struct Point { int x, y; } Point point; }
> void test() {
>     Window window;
>     alias window.point.x x;
>     // use 'x' here which is really window.point.x
> }
> 
> It makes it simpler to manipulate nested structs and their fields by
> reference without involving pointers or using with statements. AFAIK
> C++ can use references for this purpose (ala &int x =
> window.point.x;), but I guess this isn't very efficient unless the
> compiler can optimize it.

struct Window { struct Point { int x, y; } Point point; }
void test() {
    Window window;
    @property ref x() { return window.point.x; }
    // use 'x' here which is really window.point.x
}

And, yes, the compiler can and does optimize it away.

artur


More information about the Digitalmars-d mailing list