Adam Cigánek:

> What is the difference between alias and typedef?

There is a large difference: alias is just a way to give an alternative name to 
something, typically a type or instance method.

typedef creates a true new type, that's not implicitly convertible with other 
types that are _structurally_ the same (context: structural typing, nominal 
typing). typedef is now deprecated in D2, don't use it in D2, so you may ignore 
it. (But to learn the difference you may write some little experiments in D1 
(or even D2, it's not removed yet)).

On the other hand Walter has originally added typedefs to D not for sport, 
there is a need for its functionality. Many C++ programmers have asked for it 
in past. Andrei has deprecated typedef not just because Andrei loves 
library-defined features, but because typedef, as present in D1, is not 
flexible enough, and its semantics is problematic for a language that has OOP.

In a language without OOP like Pascal or C a "strong typedef" is very useful 
(Pascal has it, C doesn't have it). A simple example: in your Pascal/D1 program 
you have two functions, both have a int[5][5] matrix as argument, but those are 
two very different kinds of data. In this case you may use D1 typedef to create 
two different types of int[5][5] matrix, so if you give the wrong kind of 
matrix to one of those functions, the compiler catches the bug for you. This is 
so useful that Pascal/Ada programmers are encouraged to typedef most arrays or 
data in their programs.

Andrei ha suggested to create various kinds of library-defined "typedefs" that 
implement subtyping, supertyping, etc, but so far nothing has appeared. In the 
end some kind of typedef is and will be very useful for D, but Andrei has 
removed a feature that he regards as broken. If you don't have a feature you 
may add a better feature later in D2 or D3, while if you have a broken feature 
you will be forced to keep it forever in the language. Adding new features to a 
language is much simpler than removing them when the language is finalized. So 
removing it was the right choice, despite I now miss it...

Bye,
bearophile

Reply via email to