Closure without parameter


#1

Hi,

I need to provide a function to a c++ library and I want the user to pass a Julia function. The c++ function is like f(dy,t,y) and is called all over the place in the c++ code. I have succeeded in the past to write closures when the function definition is f(dy,t,y,param) by copying the method used in Sundials.

However I have no clue how to deal with this situation.

Does anyone have an idea?

Thank you very much,

Best regards.


#2

No you currently can’t do it


#3

You mean that no trick is known?


#4

You mean like the ParameterizedFunction constructor that encloses parameters f(t,u,p,du) into f(t,u,du)? That’s done with a call-overloaded type and is used with Sundials all the time.


#5

I think this is made possible in Sundials because the C code asks for a function rhs(t,u,p,du) where you can pass a parameter to the function rhs, and thus you pass a pointer to your julia function rhs_julia. Do I understand fully your remark @ChrisRackauckas ?

Basically, I need a closure to insert a wrapping part (see below) in the code (provided by the user) the code

function rhs_passed_to_c(Dy,t,y)
  # wrapping part
  Dy_ = unsafe_wrap(Array,Dy,N)
  y_ = ConstArray(y,N)
  # user provided code
  rhs_julia(Dy_,t,y_)
  nothing
end

I guess a macro would do but I was wondering about other solutions.