# Function wrapper for custom functions

**URL:** <https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409>\
**Category:** General Usage\
**Created:** [August 11, 2019, 10:26am UTC](https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409 "2019-08-11T10:26:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [August 11, 2019, 10:26am UTC](https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409/1 "2019-08-11T10:26:18Z")

</div>

Hi everyone,

I have 2 functions defined that are used for resampling a vector of reals:

```julia
using Distributions
using BenchmarkTools

function resampling_multinomial(weights::AbstractVector{T}, particlesN::Int) where {T<:Real}
rand(Categorical(weights), particlesN )
end

function resampling_systematic(weights::AbstractVector{T}, particlesN::Int) where {T<:Real}
#Assign cumulative Weights
uniforms = rand.( Uniform.(0, 1/particlesN) ) .+ range(0; step = 1/particlesN, length = particlesN)
path = zeros(Int, particlesN, 1)
#Some other calculations
return path
end

resampling_multinomial( [.1, .2, .3, .4], 100) #Int64[100]
resampling_systematic( [.1, .2, .3, .4], 100) # 100*1 Array{...}

```

Now I would like to define a wrapper function with a more suitable name and call either of the function based on a stringname from the arguments.

```julia
#Wrapper function for my custom functions
resample(weights::AbstractVector{T}, particlesN::Int, method::String = "multinomial") where {T<:Real} = resampling_multinomial(weights, particlesN)
resample(weights::AbstractVector{T}, particlesN::Int, method::String = "systematic") where {T<:Real} = resampling_systematic(weights, particlesN)
#Default method
resample(weights, particlesN) = resample(weights, particlesN; method = "systematic")

#NOT working
resample( [.1, .2, .3, .4], 100, "multinomial") # NOT CORRECT - 100*1 Array instead of Int64[100]
resample( [.1, .2, .3, .4], 100, "systematic") # 100*1 Array
resample( [.1, .2, .3, .4], 100) # 100*1 Array

```

Unfortunately, this is not working. I cannot dispatch the functions cause the two functions do not differentiate in any type. If I try it the way I did above the function wrapper just gets overwritten and will always use the last line of code.  
**QUESTION:** How can I define a more convenient function name and call either of my custom resampling functions based on a stringname from the arguments?

Best regards,

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 11, 2019, 11:46am UTC](https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409/2 "2019-08-11T11:46:29Z")

</div>

Why not a simple branch:

```julia
function resample(weights, particlesN; method = "systematic")
  if method == "systematic"
    resampling_systematic(...)
  else
    resampling_multinomial(...)
end

```

I suppose if you really wanted to dispatch at compile time you could define `struct Systematic end` and `struct Multinomial end` and define wrappers of the form

```julia
function resample(weights, particlesN, method :: Systematic)
  resampling_systematic(...)
end

```

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [August 11, 2019, 11:54am UTC](https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409/3 "2019-08-11T11:54:09Z")

</div>

Thank you! I think the second suggestion is the way to go for me. I did not really want to branch it as these functions will be called many times in other functions.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 11, 2019, 12:18pm UTC](https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409/4 "2019-08-11T12:18:58Z")

</div>

I’m just wondering: can the compiler infer the type of `method` while the programmer cannot?

I would check `@code_warntype` on this code if performance is important.

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [August 11, 2019, 12:37pm UTC](https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409/5 "2019-08-11T12:37:45Z")

</div>

It seems to be able to, `@code_warntype` gives the following output for the problem above:

```julia
Body::Array{Int64,2}
1 ─ %1 = invoke Main.resampling_systematic(_2::Array{Float64,1}, _3::Int64)::Array{Int64,2}
└── return %1

```

I still need to call `Multinomial()` and `Systematic()` instead of `Multinomial` and `Systematic` in the function wrapper as the latter are no structs but apart from that it is perfect.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 11, 2019, 2:19pm UTC](https://discourse.julialang.org/t/function-wrapper-for-custom-functions/27409/6 "2019-08-11T14:19:10Z")

</div>

I was suggesting to make up `struct`s that work rather like an `enum`. That is, define

```julia
struct Systematic end

```

so that you can dispatch on the function signature by passing an object of type `Systematic`.

Perhaps I misunderstood your reply, though.
