pass a funtion(with function signature) as an argument

how do i pass a function as an argument while specifying the function signature? What would be the syntax for something like this?

function foo(a::Any, goo::Function{Any, Int64})
return goo(a, 10)
end

You can’t. You can only pass generic functions which can have several methods. However, the call goo(a, 10) will pick the appropriate method of goo (or error if there is none). So you should be ok.

Jeff’s thesis is a good read: https://github.com/JeffBezanson/phdthesis/blob/master/main.pdf.

PS: check out PSA: how to quote code with backticks and welcome!

2 Likes

You can create a new parametric type that hold the input/output types as parameters. You can check that gist that I made a while ago for a similar question for an example of that (the function g at the end show how you can constrain on input/ouput parameters types).

1 Like

Note that there would generally be no performance benefit to doing this — Julia’s compiler already specializes a function to the passed argument types when it is called, including higher-order (function) arguments.

3 Likes