On Sun, May 22, 2016 at 4:24 PM, Michael Lindon
<[email protected]> wrote:
> Hi Yichao Yu,
>
> Thank you for the quick reply. Could you please elaborate on how to pass the
> data explicitly as you suggest? I don't quite understand.

julia> foo(x, y) = convert(Cdouble, x * y)
foo (generic function with 1 method)

julia> ptr = cfunction(foo, Cdouble, Tuple{Ref{Cdouble},Ref{Cdouble}})
Ptr{Void} @0x00007f32e66b1000

julia> ccall(ptr, Cdouble, (Ref{Cdouble}, Ref{Cdouble}), 1, 2)
2.0


>
> On Sunday, May 22, 2016 at 4:14:02 PM UTC-4, Yichao Yu wrote:
>>
>> On Sun, May 22, 2016 at 4:09 PM, Michael Lindon
>> <[email protected]> wrote:
>> > I have function baz which outputs the product of the input at 2:
>> >
>> > function baz{T}(x::T)
>> >         return convert(Cdouble,x*2)::Cdouble
>> > end
>> >
>> > I want to use this function in some C function that I am calling.
>> > Running
>> >
>> > julia> myptr=cfunction(baz,Cdouble,(Ref{Cdouble}))
>> > Ptr{Void} @0x00007f2623c9c1d0
>> >
>> > is fine, and I can proceed to  pass the function pointer to my C
>> > function.
>> >
>> >
>> >
>> >
>> > Say I want the product of the input and another integer. I have a higher
>> > order function bar
>> >
>> > function bar(y)
>> >         function foo{T}(x::T)
>> >                 return convert(Cdouble,x*y)::Cdouble
>> >         end
>> > end
>> >
>> > now I can get the baz function like this
>> >
>> > julia> foo=bar(2)
>> > foo (generic function with 1 method)
>> >
>> > foo has the same method as baz, and the same functionality, but
>> > cfunction
>> > results in an error:
>> >
>> > julia> myptr=cfunction(foo,Cdouble,(Ref{Cdouble}))
>> > ERROR: cfunction: no method exactly matched the required type signature
>> > (function not yet c-callable)
>> >  in cfunction at c.jl:9
>> >
>> >
>> > :(
>> >
>> >
>> > How can I pass the function pointers of the outputs of my higher order
>> > function bar to my C function?
>>
>> Closure is not supported right now. You need to pass the data
>> explicitly. (Most C API should allow that by allowing you to pass an
>> arbitrary `void*` in additional to the function pointer)
>>
>> >

Reply via email to