Is typedef an alien?

Chad J chadjoan at __spam.is.bad__gmail.com
Fri Sep 25 00:45:32 PDT 2009


Chad J wrote:
> Hmmmm, there's a lot of hating on typedef.
> 
> I've found it rather useful for at least one thing:
> 
> 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.
> }

I should also add that a ColorFormat is implicitly convertable to a
uint, so you should be able to use it as an array index to populate
tables and such too.  As such, typedefs are not completely incompatible
with their parent type, but are considered as specialization of the
parent type.



More information about the Digitalmars-d mailing list