On Sat, 07 Mar 2009 17:04:56 +1300, Andrei Alexandrescu <seewebsiteforem...@erdani.org> wrote:

Tim M wrote:
On Sat, 07 Mar 2009 06:47:40 +1300, Andrei Alexandrescu <seewebsiteforem...@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


Derelict uses typedef for functions and alias for others by convention:
"The declarations of the function pointers should be typedefed and should have the following syntax:"
http://svn.dsource.org/projects/derelict/trunk/docs/derelictify.html

It seems to be in strong use for where an alias could do the job just fine:
http://www.google.com/search?rls=en&rls=en&q=site:dsource.org+typedef&sourceid=opera&ie=utf-8&oe=utf-8


Reply via email to