Aliases
Hasan Aljudy
hasan.aljudy at gmail.com
Sun Oct 22 17:16:22 PDT 2006
Bill Baxter wrote:
> rm wrote:
>> Reiner Pope wrote:
>>
>>> 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:
>>
>>
>> you could do this:
>>
>> template KT(T)
>> {
>> union KT
>> {
>> T[2] nodes;
>> struct
>> {
>> T left;
>> T right;
>> }
>> }
>> }
>>
>> alias KT!(int) K;
>>
>> roel
>
> Or in this case he's not even after a template, so just
> union {
> alias int NodeID;
> NoteID[2] nodes;
> struct {
> NodeID left;
> NodeID right;
> }
> }
>
> But, I see your point, Reiner. 'alias foo[1] bar' expresses the intent
> of the programmer more clearly here than union, and doesn't have the
> typesafety issues.
>
> --bb
I think this is a typesafe solution:
typeof(node[1]) left;
typeof(node[2]) right;
More information about the Digitalmars-d
mailing list