D aliases vs. C typedefs

Tom Browder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 10 12:40:20 PDT 2014


I haven't found a detailed description of simple aliases.  TPDL shows
aliases of this form:

  alias TYPE NAME;

with some complex examples of D types aliased to a simpler name.

The D lang web site language reference says nothing about aliases
(except as it's described in the grammar), and the D lang wiki gives
only one simple example on page
"http://wiki.dlang.org/Converting_C_.h_Files_to_D_Modules#Types".

I need help because I'm zeroing in on properly converting C headers to
D bindings  and have found some interesting (and confusing to me)
results.

The following, uncommented-out aliases all compile with "dmd -c file.d":

// note the two forms: "alias TYPE NAME;" and "alias NAME = TYPE;"
alias int val0;            // C: typedef int val0; // okay
alias val1 = int;         // C: typedef int val1;  // okay
alias val2 = int[2];     // C: typedef int val2[2]; // a 2-element
                                   //                array of ints (int[2])?
alias int[2] val3;        // C: typedef int val3[2]; // a 2-element
array of ints (int2)?

//alias val4[2] = int[2]; // D error (okay, understandable)

// these compile but have no C equivalent that I know of, but what do
// the aliases represent?

alias int[2] val5[2];     // D: a 2-dimensional array of ints?  (int[2][2]) ?
alias int[4] val6[2];     // D:  a 2-dimensional array of ints? (int[4][2]) ?
alias int    val7[2];      // D:  a 1-dimensional array of ints? (int[2]) ?

I have marked with question marks the ones I'm not sure how to
interpret, although I have given my somewhat educated guess.
Confirmation or correction would be very helpful.

If we get a consensus, I'll update the wiki.

Thanks,

Best regards,

-Tom


More information about the Digitalmars-d-learn mailing list