Abstract function with imposed arguments and return types

I would like to define a struct where one of the field is a function. But I would also like to control the arguments and return type of that fonction. For the sake of the example, let say that function would match this signature : ::Int -> ::Float64

Currently I use the following struct that is too permissive to my taste

struct MyType
  my_function::Function
end

Is there any way to inforce the signature of `my_function` in Julia (v0.6)?

for correctness, use ::Any (or leave it off entirely)

additional discussion at Function Parameter Speculation #17168

This might be what you are looking for:

https://github.com/yuyichao/FunctionWrappers.jl

1 Like

Use

struct MyType{F} # or {F<:Function}
  my_function::F
end

if performance is important.

It would be really nice if FunctionWrappers were in Base. Very few people would have the expertise to write that package, so it would be nice if there were some sort of maintenance guarantee built in to it. As far as I know, there is no other way to do that kind of super-efficient unboxing of functions in Julia.

2 Likes

Thank you all. I have some reading to do now.

I will finish the tests and coverage of my package and try to improve the efficiency again at that point.