delegates/lambas do not pick up calling convention

Timon Gehr via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 10 07:59:49 PDT 2017


On 10.08.2017 15:22, Adam D. Ruppe wrote:
> On Wednesday, 9 August 2017 at 23:52:00 UTC, Johnson Jones wrote:
>> extern(C) delegate(void*) {}
> 
> You should very rarely use extern(C) delegate... delegate is a D type, 
> so the C function is almost certainly not actually receiving it.
> 
> Only time you'd want an extern(C) using D types like delegates, arrays, 
> strings, etc., is if the receiver is actually also written in D.
> 
> idk if this would fix your problem, but it should be changed regardless.

gdk is actually using a function with arguments of type (extern(C) int 
function(void*),void*).

Anyway, I think there are use cases for extern(C) delegates too, along 
the following lines:

alias D=extern(C) int delegate(int);

struct F(S,T...){ // this struct is compatible with C
     void* ptr;
     extern(C) S function(void*,T) funptr;
     auto opCall(T args){
         return funptr(ptr,args);
     }
}

void main(){
     int y=2;
     D dg=(x)=>x+y;
     auto f=*cast(F!(int,int)*)&dg;
     import std.stdio;
     writeln(f(3));
}


More information about the Digitalmars-d-learn mailing list