typedef: what's it good for?

Bill Baxter wbaxter at gmail.com
Wed Nov 11 09:14:06 PST 2009


On Wed, Nov 11, 2009 at 9:04 AM, grauzone <none at example.net> wrote:
> Walter Bright wrote:
>>
>> When I originally worked out ideas for D, there were many requests from
>> the C and C++ community for a 'strong' typedef, and so I put one in D. I
>> didn't think about it too much, just assumed that it was a good idea.
>>
>> Now I'm not so sure. Maybe it should be removed for D2.
>>
>> Does anyone use typedef's?
>>
>> What do you use them for?
>>
>> Do you need them?
>
> One _actual_ use of typeof is to force a different array initializer (for
> performance reasons):
>
> typedef int Foo = 1;
>
> Foo[] arr;
> arr.length = 567;
> //with int[] arr, you now had to do arr[] = 1;
>
> Also, you can easily define new exception classes:
>
> //no need to write a ctor which just calls the super ctor
> typedef Exception MyException;

Does this actually work now?  Long ago this was one of the things I
first thought I could use typedefs for, but it didn't work.  I think
it was because the compiler no longer recognizes MyException as a
subtype of Exception anymore, but it was a long time ago, so I don't
recall for sure.

--bb



More information about the Digitalmars-d mailing list