Thread overview
What is the difference between alias and typedef?
Oct 20, 2010
Adam Cigánek
Oct 20, 2010
bearophile
Oct 20, 2010
bearophile
Oct 21, 2010
Adam Cigánek
October 20, 2010
Hello there,

What is the difference between alias and typedef?

Also, is it explained somewhere? I was searching the webs and the TDPL book*, but haven't found anything :(

adam.

* sadly, it has no full text search, but since there is no mention of typedef in the index, I'm assuming it's not there.
October 20, 2010
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
October 20, 2010
I have written another note here: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=120105

Bye,
bearophile
October 21, 2010
Thanks man, this was very exhaustive reply :)

This is what i was basically thinking, just wasn't sure. Also, didn't know that typedef is deprecated. Guess that explains why is it not mentioned in the book.

Thanks,
adam.



2010/10/21 bearophile <bearophileHUGS@lycos.com>:
> 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
>