# How to dispatch method based on input function argument type

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-method-based-on-input-function-argument-type/25582>\
**Category:** General Usage\
**Created:** [June 23, 2019, 8:30pm UTC](https://discourse.julialang.org/t/how-to-dispatch-method-based-on-input-function-argument-type/25582 "2019-06-23T20:30:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 23, 2019, 8:30pm UTC](https://discourse.julialang.org/t/how-to-dispatch-method-based-on-input-function-argument-type/25582/1 "2019-06-23T20:30:52Z")

</div>

I know there is [FunctionWrapper.jl](https://github.com/yuyichao/FunctionWrappers.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

```julia
function sample(f::Function{Int})
end

function sample(f::Function{Float64})
end

```

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

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 24, 2019, 7:06am UTC](https://discourse.julialang.org/t/how-to-dispatch-method-based-on-input-function-argument-type/25582/2 "2019-06-24T07:06:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 24, 2019, 7:32am UTC](https://discourse.julialang.org/t/how-to-dispatch-method-based-on-input-function-argument-type/25582/3 "2019-06-24T07:32:25Z")

</div>

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

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

```

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

```julia
function sample(f::Function{T}) where T
    f(rand(T))
end

```

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
