typedef: what's it good for?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Nov 11 08:32:54 PST 2009


rmcguire wrote:
> Justin Johansson <no at spam.com> wrote:
>  
>> Walter Bright Wrote:
>>
>>> When I originally worked out ideas for D, there were many requests from 
>>> the C and C++ community for a 'strong' typedef, and so I put one in D. I 
>>> didn't think about it too much, just assumed that it was a good idea.
>>>
>>> Now I'm not so sure. Maybe it should be removed for D2.
>>>
>>> Does anyone use typedef's?
>>>
>>> What do you use them for?
>>>
>>> Do you need them?
>> Early on (2 months ago) when I was just getting into D I asked about typedefs 
> on this forum
>> and some discussion transpired.
>>
>> http://www.digitalmars.com/d/archives/digitalmars/D/
> Is_typedef_an_alien_96658.html#N96658
>> (btw. There are a few responses from blasts from recent pasts in that thread.)
>>
>> Anyway, grepping for typedef over my current "scripting-language in D" project 
> source
>> shows only old versions of my project using typedefs.  Accordingly it looks 
> like I have
>> since managed to convert *all* of my previous typedef incarnations to structs 
> so as to
>> take advantage of struct's support for static opCall so as to synthesize 
> "constructors"
>> (as well enabling use of struct methods).
>>
>> Maybe I didn't know enough about D back then, but the big problem with D 
> typedefs
>> (for me at least) was there was no support for typedef constructors and code 
> otherwise
>> blotted with cast-to-typedef-type is yuk in my way of thinking.
>>
>> I think there are only two sensible courses of action for D:  support typedef 
> constructors 
>> (and methods???) or remove 'em.  I'm not sure which option I prefer (is the 
> first even 
>> an option?)
>>
>> Cheers
>> Justin Johansson
>>
>>
> 
> I like typedef for making header files for c libraries.
> For example, so that you can't just pass an int to a function expecting 
> an id.

Today's typedef allows you to pass an arbitrary int literal as a typedef.

typedef int ID;
void fun(ID);

fun(42); // compiles and runs


Andrei



More information about the Digitalmars-d mailing list