need clarification: will typedef, C struct initialization, etc.

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Tue Jun 1 00:13:34 PDT 2010


On Tue, 01 Jun 2010 05:07:21 +0200, Simen kjaeraas wrote:

> Lionello Lunesu <lio at lunesu.remove.com> wrote:
> 
>> I also miss typedef. I thought D had a great opportunity to fix it.
>>
>> Take something like the Windows headers. It mostly consists of typedefs
>> for handles and whatnot. Without typedef you'd have to use alias and
>> type safety is out of the windows.
>>
>> So what would be the way to translate those Windows headers? Create a
>> unique struct for each old typedef? With alias this, and a ctor? Well,
>> if that's the way to do it now, why not make typedef a shortcut for
>> exactly that!?
>>
>> IIRC typedef is gone because you and Walter could not agree whether it
>> had to be a subtype or a supertype of the typedef'ed type. For me it's
>> rather simple: I want to introduce a new type in such a way that it
>> helps me prevent mistakes, ie. passing one handle when the function
>> wants another, even though both are based on void*, or whatever.
>>
>> Bring typedef back!
> 
> struct Typedef( T ) {
>      T payload;
>      alias payload this;
> }
> 
> alias Typedef!int myInt;
> 
> There you go.

Nice. :) But it isn't quite a typedef, because if you do

  alias Typedef!int myInt;
  alias Typedef!int yourInt;

then myInt and yourInt will refer to the same type.  How about this 
instead:

  mixin template Typedef(T, string name)
  {
      mixin("struct "~name~" { T payload; alias payload this; }");
  }

Then,

  mixin Typedef!(int, "myInt");
  mixin Typedef!(int, "yourInt");

will define two distinct types.


If we had Andrei's (or was it Walter's?) "new alias" feature it would be 
even cooler:

  mixin template Typedef(T, new alias name)
  {
      struct name { T payload; alias payload this; }
  }

  mixin Typedef!(int, myInt);
  mixin Typedef!(int, yourInt);

:)

-Lars


More information about the Digitalmars-d mailing list