Strange allocations with default positional argument and static arrays

I will try to look at the things you mentioned, thank you. As I said, I’m having trouble figuring out what to look for, so it’s much appreciated.

I am currently using dispatch. The thing is, that some methods might only need the value, some need the first derivative and some need the second. This means that since I’m getting allocations whenever I have more than one method, the user has to do the following

function zero_order_f(x)
    ...
end
function first_order_f(x, df) # or (df, x)
    ...
end
function second_order_f(x, df, ddf)
    ...
end

to avoid allocations! What I had hoped to be able to do was to just ask users to define methods with the appropriate number of arguments, and then they could simpy write

# if they know they only need zero order
function my_f(x)
   ...
end

# If they want to be able to use up to first order methods
function m_ff(x, df=nothing)
   ...
end

# if they want to be able to use up to second order
function m_fff(x, df=nothing, ddf=nothing)
   ...
end

This basically means that if I want users to be able to a) have zero allocations for static arrays and b) be able to input the same objective across methods, they always have to define the function as

function my_function(x, df, ddf)
    ...
end

even if they just want to use simulated annealing or nelder mead.

So I would really appreciate if I could instead ask them to provide a function that should at least have a method for (x) it would also need a method for (x, df) if they want to use first order methods and it should also have a method for (x, df, ddf) if they want to use newton’s method. But right now, it appears that my code will allocate if I ask them to do this. So I am “stuck” with the “always define three positional arguments even if you plan to ignore the two last ones”, and in my library code I have to write objective(x, nothing, nothing) if I just want to evaluate the value, instead of having objective(x) pick the appropriate method.

Note, in the existing Optim.jl I wouldn’t care, because that doesn’t support StaticArrays at all, but I’m trying to write it in a way such that people can get non-allocating calls if they want.