@cfunction with variable arguments

ccall

#1

I am currently trying to adapt some old code with deprecated features for julia 1.0.
Currently, I am stuck trying to implement the new @cfunction macro functionality.

The code I’m trying to adapt is the following:

function create_cfunction(f, r, a)
# run some checks
    cfunction(f, r, a)
end

which is currently called with a function f, its return type r and a tuple of arguments, eg.

create_cfunction(f, Float64, (Float64, Float64))

First of all, I corrected the code according to the first warning and changed the corresponding line successfully to

cfunction(f, r, Tuple{a...})

However, the second deprecation warning mentioned the change to the macro @cfunction. Simply changing to that however doesn’t work, as the macro seems to expect a literal tuple instead of a variable one as is being used in the code above. Is there a method of working around this restriction or is trying to do that disapproved of (eg. for performance reasons)?


#2

Does the following, from the docstring of @cfunction, help?

Note that the argument type tuple must be a literal tuple, and not a tuple-valued variable or expression (although it can include a splat expression). And that these arguments will be evaluated in global scope during compile-time (not deferred until runtime). Adding a ‘$’ in front of the function argument changes this to instead create a runtime closure over the local variable callable.


#3

Thank you for hinting at the docstring, I have in fact only read the corresponding manual chapter but not the docstring itself as it seems…
However, since this is regarding only the function argument and not the return value and argument tuple, it does not help me at all.

To me it also seems ambiguous whether or not to use @eval in front of this call, as the docstring just says “Adding a ‘$’ in front of the function argument”, whereas the manual section does similar to the ccall arguments using @eval (albeit to the name and library arguments - it does however state that @cfunction usage is “similar”)
Again to clarify: cfunction(f, r, Tuple{a...}) does work and a is taking values like (Float64,) or (Float64,Float64).
As I was unsure about using @eval or not, I tried both and it also failed, this time at runtime (at function call) with an ArgumentError.


#4

It seems like I may have posted into a wrong subforum. I would like to kindly ask a privileged user to move my topic to the correct subforum and delete this message as it has then become irrelevant.