Aliases

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Sun Oct 22 01:40:23 PDT 2006


What's the purpose of the restriction of alias in the specs -- 'Aliases 
cannot be used for expressions'; it prevents some useful things like this:

   int[2] nodes;
   alias nodes[0] left;
   alias nodes[1] right;

which currently has to be written in a much more hackish way:

   union {
     int[2] nodes;
     struct {
        int left;
        int right;
     }
   }

This is less reliable because if you decide to change the datatype of 
nodes, you could easily stuff up:

   union {
     long[2] nodes;
     struct {
        int left; // oops, forgot to change this. Now we've sliced the 
first value
        int right;
     }
   }

(Alternatively, you could implement this in an even more verbose way, 
which doesn't support things like left++:
    int[2] nodes;
    int left() { return nodes[0]; }
    int left(int val) { return nodes[0] = val; }
    int right() { return nodes[1]; }
    int right(int val) { return nodes[1] = val; }
)

It could also be used for function forwarding, as Frank Benoit requested 
earlier ('Feat. RQ: delegation alias'):

   class Foo
   {
     void DoSomething() {...}
   }

   class FooHandle
   {
     Foo myFoo;
     alias myFoo.DoSomething DoSomething;
   }


Maybe someone can explain why this restriction is necessary.

Cheers,

Reiner



More information about the Digitalmars-d mailing list