typedefs are useless
Steven Schveighoffer
schveiguy at yahoo.com
Mon Dec 3 10:08:49 PST 2007
This may have arisen somewhere before, but...
Let's say I want a way to create a type that's like a long, but is not
implicitly convertable from a long.
I can do:
typedef long mytype;
However, I can't create literals of this type. so if I want to initialize a
mytype value to 6, I have to do:
mytype x = cast(mytype)6L;
And if I want to scale a mytype value, I can do:
x *= 6;
but if I wanted to create a scaled value of another mytype value, this is
illegal:
x = y * 6;
So in order to get this to work, I have to do:
x = y * cast(mytype)6;
which is really cumersome, when what I want is a integral type that is
semantically different from the integral type it's based on. But I want
literals of the base type to implicitly convert to my type so I don't have
these ugly casts.
Note that an enum is EXACTLY the same as a typedef, with the added feature
that an enum can have const values of itself accessible as members. In that
regard, why would anyone use a typedef when it is a less-functional version
of an enum?
in short, I would like the compiler to auto promote a literal to a typedef
that was based on that literal's type. Not sure if this is feasible, but
without something like this, there is no reason to have a typedef keyword.
I can use enum instead.
-Steve
More information about the Digitalmars-d
mailing list