www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias and extern(C) for callbacks

reply "Carl Sturtivant" <sturtivant gmail.com> writes:
The D language page on interfacing to C
http://dlang.org/interfaceToC.html
says under 'Callbacks' that e.g.

   alias extern(C) int function(int, int) Callback;  // D code

is OK, so I am wondering if I can factor out the extern(C) from 
aliases as follows

   extern(C) {
     alias int function(int, int) Callback;
     // more aliases
   }

Is Callback here a name for the same type as if it was defined by 
the following?

   alias extern(C) int function(int, int) Callback;
Aug 22 2013
parent reply "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
On Thursday, 22 August 2013 at 23:55:44 UTC, Carl Sturtivant 
wrote:
 Is Callback here a name for the same type as if it was defined 
 by the following?

   alias extern(C) int function(int, int) Callback;
Yes. If you want to make sure, you can use a trait: import std.traits; extern(C) { void test1() { } } void test2() { } static assert(functionLinkage!test1 == "C"); static assert(functionLinkage!test2 != "C"); // D calling convention
Aug 22 2013
next sibling parent "Carl Sturtivant" <sturtivant gmail.com> writes:
On Friday, 23 August 2013 at 00:00:13 UTC, Andrej Mitrovic wrote:
 On Thursday, 22 August 2013 at 23:55:44 UTC, Carl Sturtivant 
 wrote:
 Is Callback here a name for the same type as if it was defined 
 by the following?

  alias extern(C) int function(int, int) Callback;
Yes. If you want to make sure, you can use a trait: import std.traits; extern(C) { void test1() { } } void test2() { } static assert(functionLinkage!test1 == "C"); static assert(functionLinkage!test2 != "C"); // D calling convention
Nice! Thank you.
Aug 22 2013
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-08-23 02:00, Andrej Mitrovic wrote:

 static assert(functionLinkage!test2 != "C");  // D calling convention
Or Windows, Pascal or C++. -- /Jacob Carlborg
Aug 22 2013
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 8/23/13, Jacob Carlborg <doob me.com> wrote:
 On 2013-08-23 02:00, Andrej Mitrovic wrote:

 static assert(functionLinkage!test2 != "C");  // D calling convention
Or Windows, Pascal or C++.
I'd prefer if functionLinkage returned an enum, it's too easy to write "c" instead of "C" in some generic code and have a static if branch not taken.
Aug 23 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-08-23 13:10, Andrej Mitrovic wrote:

 I'd prefer if functionLinkage returned an enum, it's too easy to write
 "c" instead of "C" in some generic code and have a static if branch
 not taken.
Yeah, that's a good point. -- /Jacob Carlborg
Aug 23 2013