www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Extern Keyword for Function Type

reply "Jeroen Bollen" <jbinero gmail.com> writes:
exten(C) {
     testFunction(int function(int));
}

testFunction now requires an external function as parameter. It 
can't be called with a pointer to a D function.

Logical Solution:

extern(C) {
     testFunction(extern(D) int function(int)); // DOES NOT COMPILE
}

How do you fix this without moving it outside the global extern 
block?
Apr 15 2014
next sibling parent "Jeroen Bollen" <jbinero gmail.com> writes:
On Tuesday, 15 April 2014 at 20:15:42 UTC, Jeroen Bollen wrote:
 exten(C) {
     testFunction(int function(int));
 }

 testFunction now requires an external function as parameter. It 
 can't be called with a pointer to a D function.

 Logical Solution:

 extern(C) {
     testFunction(extern(D) int function(int)); // DOES NOT 
 COMPILE
 }

 How do you fix this without moving it outside the global extern 
 block?
Whoops, assume testFunction is defined as "void testFunction(...)"
Apr 15 2014
prev sibling next sibling parent reply "Dicebot" <public dicebot.lv> writes:
C has no knowledge of D ABI so this can't work. If you just want 
to store D function pointer to later retrieve it and call from D 
code, you can as well store it as void* (or extern(C) with 
similar signature to preserve part of type) and cast upon 
interfacing.
Apr 15 2014
parent "Jeroen Bollen" <jbinero gmail.com> writes:
On Tuesday, 15 April 2014 at 20:19:36 UTC, Dicebot wrote:
 C has no knowledge of D ABI so this can't work. If you just 
 want to store D function pointer to later retrieve it and call 
 from D code, you can as well store it as void* (or extern(C) 
 with similar signature to preserve part of type) and cast upon 
 interfacing.
Hmm so the function passed should be declared as extern C but be defined nonetheless? That makes sense.
Apr 15 2014
prev sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 04/15/14 22:15, Jeroen Bollen wrote:
 exten(C) {
     testFunction(int function(int));
 }
 
 testFunction now requires an external function as parameter. It can't be
called with a pointer to a D function.
 
 Logical Solution:
 
 extern(C) {
     testFunction(extern(D) int function(int)); // DOES NOT COMPILE
 }
 
 How do you fix this without moving it outside the global extern block?
extern(C) { extern (D) alias FP = int function(int); void testFunction(FP); } artur
Apr 15 2014
parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 15 April 2014 at 22:32:10 UTC, Artur Skawina wrote:
    extern(C) {
        extern (D) alias FP = int function(int);
        void testFunction(FP);
    }

 artur
Still does not allow you to actually call it from C side, so this is somewhat confusing. I'd really just go with void*
Apr 15 2014