typedef: what's it good for?
Chad J
chadjoan at __spam.is.bad__gmail.com
Wed Nov 11 13:13:26 PST 2009
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?
>
A little.
> What do you use them for?
>
typedef uint ColorFormat;
enum : ColorFormat
{
RGB,
RGBA,
HSV,
CMYK,
// etc
}
Now you have an enum that is type safe but doesn't require a qualified
name to use.
void convertToFormat( SomeTextureType someTexture, ColorFormat fmt )
{
// etc
}
void main()
{
// ...
auto foo = new SomeTextureType(...);
convertToFormat(foo, 1); // Error, 1 is not a ColorFormat
convertToFormat(foo, ColorFormat.RGBA ); // Nope
convertToFormat(foo, RGBA); // This works.
}
Since ColorFormat is implicitly convertable to a uint, you should be
able to use it as an array index to populate tables and such too.
> Do you need them?
Not really.
It would be nice to have this kind of enum though. I think it was even
mentioned in D con 2007 IIRC.
More information about the Digitalmars-d
mailing list