www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D aliases vs. C typedefs

reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Jun 10 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/10/2014 12:40 PM, Tom Browder via Digitalmars-d-learn wrote:

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

    alias TYPE NAME;
That's the old syntax. As you note below, the newer syntax uses the = sign. My alias chapter uses the new syntax as well: http://ddili.org/ders/d.en/alias.html
 // note the two forms: "alias TYPE NAME;" and "alias NAME = TYPE;"
 alias int val0;            // C: typedef int val0; // okay
Yes, the old syntax.
 alias val1 = int;         // C: typedef int val1;  // okay
Yes, new syntax.
 alias val2 = int[2];     // C: typedef int val2[2]; // a 2-element
                                     //                array of ints 
(int[2])? val2 means a 2-element static array of ints: static assert(is (val2 == int[2])); static assert(is (typeof(val2.init[0]) == int));
 alias int[2] val3;        // C: typedef int val3[2]; // a 2-element
 array of ints (int2)?
Same as above. int[2] means a 2-element static array of ints.
 //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]) ? Pretty strange. :) pragma(msg, val5); outputs this: 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 don't know whether those are legal. I hope not. :) Ali
Jun 10 2014
parent reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, Jun 10, 2014 at 3:05 PM, Ali Çehreli
<digitalmars-d-learn puremagic.com> wrote:
 On 06/10/2014 12:40 PM, Tom Browder via Digitalmars-d-learn wrote:
...
 That's the old syntax. As you note below, the newer syntax uses the = sign.
 My alias chapter uses the new syntax as well:

   http://ddili.org/ders/d.en/alias.html
I forgot to look there, sorry.
 // 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])
 ?
Pretty strange. :) pragma(msg, val5); outputs this: int[2][2]
Okay, checks with my guess.
 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 don't know whether those are legal. I hope not. :)
I agree, but they compile! If they are not legal, why am I getting no errors? I'll try the pragma on the others. Thanks, Ali. -Tom
Jun 10 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/10/2014 01:18 PM, Tom Browder via Digitalmars-d-learn wrote:

 alias int[2] val5[2];     // D: a 2-dimensional array of ints? 
(int[2][2])
 ?
Pretty strange. :) pragma(msg, val5); outputs this: int[2][2]
Okay, checks with my guess.
 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 don't know whether those are legal. I hope not. :)
I agree, but they compile! If they are not legal, why am I getting
no errors? I think they are actually legal: This is D's support of C-style array declaration acting the same in alias declaration: void main() { int a[2]; // C-style int[2] b; // D-style static assert(is (typeof(a) == typeof(b))); alias int A[2]; // C-style alias int[2] B; // old D-style alias C = int[2]; // D-style static assert(is (A == B)); static assert(is (B == C)); } Ali
Jun 10 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 10 June 2014 at 20:33:03 UTC, Ali Çehreli wrote:
 I think they are actually legal: This is D's support of C-style 
 array declaration acting the same in alias declaration:

 Ali
C-style array declaration has got to be one of C's *worst* abominations. There's no real *technical* rationale for it either. I am ***INCREDIBLY*** glad D's stance is simply "depth first left to right". It *supports* C style, but unless you are copy pasting some C code, you'd have to be mad in your head to actually ever use it. Honestly, try to declare: *A two element array of pointers to int *A pointer to a two element array of ints Tip: the solutions are in this set: int *arr1[2]; int (*arr2)[2]; int *(arr3[2]); Also, the syntax is *so* horrible, the syntax is actually deprecated in D. And that's saying a lot, when you know how much Walter hates breaking code...
Jun 10 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/10/2014 01:48 PM, monarch_dodra wrote:

 I am ***INCREDIBLY*** glad D's stance is simply "depth first left to
 right".
I completely agree.
 It *supports* C style, but unless you are copy pasting some C
 code, you'd have to be mad in your head to actually ever use it.

 Honestly, try to declare:
 *A two element array of pointers to int
 *A pointer to a two element array of ints

 Tip: the solutions are in this set:
 int *arr1[2];
 int (*arr2)[2];
 int *(arr3[2]);
Madness! :)
 Also, the syntax is *so* horrible, the syntax is actually deprecated in
 D. And that's saying a lot, when you know how much Walter hates breaking
 code...
Going off topic, browse this thread for more fun: :p http://stackoverflow.com/questions/10758811/c-syntax-for-functions-returning-function-pointers int (*(*(*f3)(int))(double))(float); "f3 is a ..." Ali
Jun 10 2014
parent "Chris Cain" <zshazz gmail.com> writes:
On Tuesday, 10 June 2014 at 22:00:34 UTC, Ali Çehreli wrote:
 http://stackoverflow.com/questions/10758811/c-syntax-for-functions-returning-function-pointers

   int (*(*(*f3)(int))(double))(float);

 "f3 is a ..."

 Ali
f3 is a pointer to a function taking an int returning a pointer to a function taking a double returning a pointer to a function taking in a float and finally returning an int. Read via spiral method. Someone else once said (I think it was Walter?) that you declare it how you use it. But in this case, I don't see the baseball bat hitting the programmer who wrote the code, so I'm not sure if that's true...
Jun 10 2014