typedef

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Mar 6 20:04:56 PST 2009


Tim M wrote:
> On Sat, 07 Mar 2009 06:47:40 +1300, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> I tried typedef in two instances, to sadly conclude that it's not 
>> useful as a true abstraction mechanism. The good news is that it's 
>> very close to being useful.
> 
> I've never really found typedefs of any use. You say it makes it clearer 
> that you are dealing with IDType objects. But they are not objects. If 
> you had used objects you could have defined the constuctor to get the 
> syntax you are looking for because taking away this syntax:
> 
> IDType id = 5;
> 
> Should also remove the following syntaxes to keep D simple:
> 
> *=
> +=
> -+
> %=
> 
> etc..
> 
> 
> So you end up with a type that stores int data but cant be used for 
> arithmetic. What good would that be keeping in mind that D already has 
> enums?

Well I was wondering whether others have success stories about typedef 
to share. Here's an example that would have made it very handy for me to 
use a good typedef.

I have a machine translation app that deals with sentences in two 
languages. Furthermore, in the target language there are several 
hypotheses per sentence. I undergo intensive manipulation of these 
things, and found bugs because I confuse e.g. the IDs of sentences in 
different languages. So I index in a hash for one language with an ID 
from another language, which is a bug.

So I though, alright, let me typedef IDs so they never mix up:

typedef uint SourceSntID;
typedef uint TargetSntID;
typedef uint HypothesisID;

Things did improve considerably but I found it annoying that these IDs, 
which are supposed to be essentially independent types, convert to and 
from uint at the drop of a hat. I could define structs for them but 
that's onerous - like dozens of lines instead of one line.


Andrei



More information about the Digitalmars-d mailing list