Library Typedefs are fundamentally broken

Don via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 23 07:43:40 PDT 2014


On Monday, 22 September 2014 at 14:56:26 UTC, Andrei Alexandrescu 
wrote:
> On 9/22/14, 2:39 AM, Don wrote:
>> On Sunday, 21 September 2014 at 18:09:26 UTC, Andrei 
>> Alexandrescu wrote:
>>> On 9/21/14, 8:29 AM, ketmar via Digitalmars-d wrote:
>>>> On Sun, 21 Sep 2014 08:15:29 -0700
>>>> Andrei Alexandrescu via Digitalmars-d 
>>>> <digitalmars-d at puremagic.com>
>>>> wrote:
>>>>
>>>>> alias Int1 = Typedef!(int, "a.Int1");
>>>>> alias Int2 = Typedef!(int, "b.Int2");
>>>> ah, now that's cool. module system? wut? screw it, we have 
>>>> time-proven
>>>> manual prefixing!
>>>
>>> Use __MODULE__. -- Andrei
>>
>>
>> Yes, but you're advocating a hack.
>
> Oh but I very much disagree.

Now you are scaring me. It worries me that this kind of 
"solution" can be viewed as acceptable. It's the kind of hacky 
code I left C++ to escape from.

People in this thread said it was "ugly" and you dismissed that. 
But this isn't just a matter of personal aesthetics.
If you want something objective, it's not DRY, and it's verbose 
in a non-trivial way. The hacky design leads to error-prone code.
eg you can easily get a copy-paste bug because it's not DRY.

alias HMENU = Typedef!(void*, __MODULE__ ~ ".HMENU");
alias HFONT = Typedef!(void*, __MODULE__ ~ ".HMENU"); // oops


>> The original premise does seem to be
>> correct: library Typedefs are fundamentally broken. The 
>> semantics of
>> templates does not match what one expects from a typedef: ie, 
>> declaring
>> a new, unique type.
>>
>> If you have to pass __MODULE__ in, it's not really a library 
>> solution.
>> The user code needs to pass in a nasty implementation detail 
>> in order to
>> get a unique type.
>
> How many libraries did you use that came with no idioms for 
> their usage?

Describing this as an "idiom" is extremely generous. My standards 
are higher.


>> And it does seem to me, that because it isn't possible to do a 
>> proper
>> library typedef, you've attempted to redefine what a Typedef 
>> is supposed
>> to do. And sure, it you remove the requirement to create a 
>> unique type,
>> Typedef isn't broken.
>
> You're two paragraphs away from "library Typedefs are 
> fundamentally broken". Now which one is it?

Phobos' Typedef is fundamentally broken, and that your claim that 
it is not, relies on moving the goalposts.

>> But then it isn't very useful, either. You can't,
>> for example, use it to define the various Windows HANDLEs 
>> (HMENU, etc),
>> which was one of the most successful use cases for D1's 
>> typedef.
>
> alias HMENU = Typedef!(void*, __MODULE__ ~ ".HMENU");
>
> So please s/can't/can't the same exact way built-in typedef 
> would have done it/.

No. You can hammer nails in using a rock, but I'm not prepared to 
accept a rock as a kind of hammer. It's not a tool that belongs 
in any toolbox.

My assertion is, there are no use cases for Phobos's Typedef.
You're always better off doing something else.


More information about the Digitalmars-d mailing list