Say I am interfacing to C.
Here is an wrapping function to the interface.
@property extern(C) void onEvent(void function(InterfaceStruct*, int, int, int) nothrow callback)
{
interfaceSetCallback(handle, callback);
}
All good.
wrapper.onEvent = function void (InterfaceStruct*, int x, int y, int z) nothrow
{
if (x == 11) doSomething();
};
Uh oh:
Error: function foo.bar.onEvent (void function(InterfaceStruct*, int, int, int) nothrow callback) is not callable using argument types (void function(InterfaceStruct* _param_0, int x, int y, int z) nothrow @nogc @safe)
So, it wants me to have the function literal being extern(C). So how can I do that? I can't find any way to do so.
Instead of providing the whole function definition you can simply assign onEvent using
wrapper.onEvent = (a, x, y, z)
{
if (x == 11) doSomething();
};
D will automatically assign it the correct type.
Also your code should actually give you a syntax error because extern(C) is actually not allowed when using it for a function pointer definition.
You could alternatively define an alias for the function pointer type and cast the assignment to it like this:
alias EventCallback = extern(C) void function(InterfaceStruct*, int, int, int) nothrow;
@property extern(C) void onEvent(EventCallback callback)
{
interfaceSetCallback(handle, callback);
}
// ...
wrapper.onEvent = cast(EventCallback) function void(InterfaceStruct*, int x, int y, int z) nothrow
{
if (x == 11) doSomething();
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.