Is typedef an alien?

bearophile bearophileHUGS at lycos.com
Thu Sep 24 12:05:02 PDT 2009


Aenigmatic:

>Is typedef (in D) a C/C++ legacy or is the dear orphan now adopted as a first-class citizen in the US of D? -- Yours truly, Justin Johansson<

The semantics of D typedef comes from Pascal, not from C.
It's useful, but unfortunately it's not a true first-class feature in D, its purposes are mostly pre-OOP.
You can use it for example to pass a type-specific fixed-size matrix to several functions, and for similar purposes. Not-ex-Pascal programmers may find it less useful (as nested functions).

I'd even like to have Pascal-inspired ranged integral values in D :-) 
int (1 .. 20) x;

They give some safety, and in the wild I've already seen D code like:
int foo; // values 0 .. 20
(There are several modern ways to generalize this idea of the type system, but in many programs such integral ranges are enough).

Often the comments you can find in programs written in old languages can be written as annotations and syntax in modern languages. Generally it's good to have a syntax to tell the compiler some of the semantics you want to put into the comments.

Bye,
bearophile



More information about the Digitalmars-d mailing list