Library Typedefs are fundamentally broken

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 19 20:14:08 PDT 2014


On 09/20/2014 02:29 AM, Andrei Alexandrescu wrote:
> On 9/19/14, 5:28 PM, Timon Gehr wrote:
>> On 09/19/2014 04:59 PM, Andrei Alexandrescu wrote:
>>> On 9/18/14, 10:53 PM, bearophile wrote:
>>>> Andrei Alexandrescu:
>>>>
>>>>> Wyatt:
>>>>>> (I wouldn't consider the cookie parameter a better solution; I would
>>>>>> consider it a wart.)
>>>>>
>>>>> That's the right solution.
>>>>
>>>> The cookie parameter is a ugly wart.
>>>
>>> No. -- Andrei
>>>
>>
>> Yes.
>
> No. :o) -- Andrei
>

To substantiate: It does the wrong thing (same typedef for same base 
type) by default and doing the right thing (emulating nominal typing) 
may require quite some effort in general (e.g. concatenate the mangled 
names of all enclosing template instantiations) while remaining 
non-modular (those cookie strings are global identifiers).

I.e. Typedef is fail. Won't use. Declaring a new struct and using Proxy, 
perhaps encapsulated using a mixin template is less trouble (quick hack, 
results in bad error messages):

import std.typecons;

mixin template WorkingTypedef(string i,T,T init=T.init)/+if(...)+/{
     // workaround for https://issues.dlang.org/show_bug.cgi?id=13500
     mixin std.typecons.Typedef!(T,init);
     mixin(`alias `~i~` =Typedef;`);
}
mixin WorkingTypedef!("Name1",int);
mixin WorkingTypedef!("Name2",int,2);

void main(){
     Name1 foo;
     Name2 bar;
     foo=bar; // error
}


Feel free to disagree, but I don't see how one can.


More information about the Digitalmars-d mailing list