How to dispatch method based on input function argument type

I know there is FunctionWrapper.jl but is it possible to use some design pattern to workaround this without letting user write the wrapper themselves?

e.g I’d like to dispatch the following

function sample(f::Function{Int})

function sample(f::Function{Float64})

this is useful since sometimes the argument of this callback function is generated inside another function.

There is no way to dispatch on methods like this without some kind of wrapping like FunctionWrappers.

However, to me, trying to do this signals some kind of design failure since f is a generic function and the methods of it should just be implementations of that generic function. Could you give an example when this is wanted?

1 Like

Yes I’m trying to implement a sampler that takes a function as input and output a probability, but its sampling space is on some custom array of spins, which means I can’t assume the input of that function is limited to a specific type. One way to workaround is to define

function sample(f, space::SomeSpaceType)
    x = f(generate_from(space))
    # rest of the sampling

but the info of space is actually contained as the input type of f, so if I know f’s input then I don’t need this extra argument at all.

Edit: if I know the input type of the method, I could just generate x from based on its type, e.g

function sample(f::Function{T}) where T

it is not the methods of f but the methods of f’s input, thus I think I need to know which method is dispatched to sample’s callback f, when sample gets called