I am trying to get cfunction
to work with local functions and types
https://github.com/JuliaDiffEq/Sundials.jl/commit/54e7e18788fed1b10c2bb69fcc375f45f0de0177
I think I am having some GC segfaults because of this. @rdeits said he was playing around with this and functions can be interpolated into the macro:
julia> function local_cfunction(f)
@cfunction($f, Float64, (Int,))
end
local_cfunction (generic function with 1 method)
julia> local_cfunction(x -> x + 1.0)
Base.CFunction(Ptr{Nothing} @0x00007f41f46d9f80, getfield(Main, Symbol("##3#4"))(), Ptr{Nothing} @0x0000000000000000, Ptr{Nothing} @0x0000000000000000)
but the types cannot, and so those need to be handled via an @generated
function:
julia> @generated function local_cfunction(f, ret, arg)
quote
@cfunction($(Expr(:$, :f)), $ret, ($arg,))
end
end
local_cfunction (generic function with 2 methods)
julia> local_cfunction(x -> x + 1.0, Float64, Int)
Base.CFunction(Ptr{Nothing} @0x00007f41df522f40, getfield(Main, Symbol("##6#7"))(), Ptr{Nothing} @0x0000000000000000, Ptr{Nothing} @0x0000000000000000)
It seems like that is a quite complicated solution though. Is there something simpler or is this @generated
helper function required everywhere?