typedef: what's it good for?

grauzone none at example.net
Wed Nov 11 09:20:51 PST 2009


Bill Baxter wrote:
> 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.

I think it does. There was a bug that prevented this from working, but 
it was fixed:
http://d.puremagic.com/issues/show_bug.cgi?id=1285

Anyway, D should do something about those annoying ctors semantics. I 
propose we use exactly the same model as Object Pascal / Delphi did.

> --bb



More information about the Digitalmars-d mailing list