Library Typedefs are fundamentally broken

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 19 21:52:57 PDT 2014


On 9/19/14, 8:14 PM, Timon Gehr wrote:
> 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).

This is wrong but probably not worth fighting. Consider:

alias A = Typedef!float;
alias B = Typedef!float;

By basic language rules, A and B are identical. Making them magically 
distinct would be surprising and would require explanation. It's as if 
someone writes:

struct Point { int x, y; }
alias A = Point;

and then later in the same module

alias B = Point;

and complains that the compiler didn't automagically distinguish between 
the two.

Human-readable cookies are exactly the solution: distinct human-readable 
moniker that distinguish the types.

alias A = Typedef!(float, "dollar");
alias B = Typedef!(float, "euro");

They will be distinct to the human and compiler alone.

> I.e. Typedef is fail. Won't use.

Typedef is win. Feel free to not 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.

Your argument has been destroyed.


Andrei



More information about the Digitalmars-d mailing list