using a typedefed variable with library classes
Charles Hixson
charleshixsn at earthlink.net
Sun Jan 11 18:09:15 PST 2009
bearophile wrote:
> Bill Baxter:
>
>> The first time I tried to use it my thought was to do
>> typedef Exception MyException;
>> To create a different exception type. That doesn't work so I kinda
>> just said, eh whatever, this typedef stuff doesn't work yet.
>
> You are right, typedef was invented before classes and OOP, so what you say doesn't work :-)
> But I think typedef may be modified to work as you want too, that is to define a subtype (so the runtime typeinfo of MyException too has to be different!). I/we can ask Walter what he thinks about this.
>
>
>> Now I can't accidentally assign a vertex handle to a face handle.
>> That could be useful.
>
> Right. But so far I don't see problems in extending the semantics of typedef to work with OOP too. Maybe other people here can spot a problem. I presume Walter doesn't read this newsgroup, so I can post a summary in the main D group too, later.
>
> Bye,
> bearophile
Well, my use case just involves being able to use library function with
the proper base type. (I.e., int instead of long or byte when I do
typedef int LocalType;
LocalType t;
File f;
f.write(t);
I'll grant that I *can* use alias for such a purpose, but that doesn't
distinguish between LocalType and ArrayIndex in routines defined for
those types. E.g.:
typedef int ArrayIndex;
void zeroIndex(out ArrayIndex ai) { ai = 0; }
zeroIndex(t);
Should throw an error. If I'm using alias it doesn't (and shouldn't);
More information about the Digitalmars-d-learn
mailing list