digitalmars.D.learn - D aliases vs. C typedefs
- Tom Browder via Digitalmars-d-learn (32/32) Jun 10 2014 I haven't found a detailed description of simple aliases. TPDL shows
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (20/36) Jun 10 2014 That's the old syntax. As you note below, the newer syntax uses the =
- Tom Browder via Digitalmars-d-learn (9/27) Jun 10 2014 ...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (18/36) Jun 10 2014 (int[2])
- monarch_dodra (17/20) Jun 10 2014 C-style array declaration has got to be one of C's *worst*
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (9/23) Jun 10 2014 Madness! :)
- Chris Cain (8/12) Jun 10 2014 f3 is a pointer to a function taking an int returning a pointer
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
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; // okayYes, the old syntax.alias val1 = int; // C: typedef int val1; // okayYes, 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
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.htmlI forgot to look there, sorry.Okay, checks with my guess.// 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]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. -Tomalias 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. :)
Jun 10 2014
On 06/10/2014 01:18 PM, Tom Browder via Digitalmars-d-learn wrote:(int[2][2])alias int[2] val5[2]; // D: a 2-dimensional array of ints?(int[2])Okay, checks with my guess.?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?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)); } AliI agree, but they compile! If they are not legal, why am I getting?I don't know whether those are legal. I hope not. :)
Jun 10 2014
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: AliC-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
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
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 ..." Alif3 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