# Does FunctionWrappers.jl support parametric polymorphism?

**URL:** <https://discourse.julialang.org/t/does-functionwrappers-jl-support-parametric-polymorphism/66408>\
**Category:** General Usage\
**Tags:** type, function\
**Created:** [August 14, 2021, 9:58pm UTC](https://discourse.julialang.org/t/does-functionwrappers-jl-support-parametric-polymorphism/66408 "2021-08-14T21:58:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 14, 2021, 9:58pm UTC](https://discourse.julialang.org/t/does-functionwrappers-jl-support-parametric-polymorphism/66408/1 "2021-08-14T21:58:02Z")

</div>

```julia
julia> (FunctionWrapper{T, T} where T)(x -> x + 1)
ERROR: MethodError: no method matching (FunctionWrapper{T, T} where T)(::var"#3#4")

julia> (FunctionWrapper{T, Tuple{T}} where T)(x -> x + 1)
ERROR: MethodError: no method matching (FunctionWrapper{T, Tuple{T}} where T)(::var"#5#6")

```

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [August 14, 2021, 10:57pm UTC](https://discourse.julialang.org/t/does-functionwrappers-jl-support-parametric-polymorphism/66408/2 "2021-08-14T22:57:05Z")

</div>

I can run code like this, though I don’t know if it’s what you need: (here the only polymorphism is in an ordinary function which calls the FunctionWrapper object, but the FunctionWrappers themselves have concrete argument types)

```julia
julia> using FunctionWrappers: FunctionWrapper

julia> function apply_func(f::FunctionWrapper{T, Tuple{T}}, x::T) where {T}
           f(x)
       end
apply_func (generic function with 1 method)

julia> f1 = FunctionWrapper{Int, Tuple{Int}}(x -> x+1);

julia> f2 = FunctionWrapper{Float64, Tuple{Float64}}(x -> x+1);

julia> apply_func(f1, 9)
10

julia> apply_func(f2, 9.0)
10.0

julia> apply_func(f1, 9.0) # violates the type requirement of apply_func
ERROR: MethodError: no method matching apply_func(::FunctionWrapper{Int64, Tuple{Int64}}, ::Float64)
Closest candidates are:
  apply_func(::FunctionWrapper{T, Tuple{T}}, ::T) where T at REPL[11]:1
Stacktrace:
 [1] top-level scope

```

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [August 14, 2021, 11:41pm UTC](https://discourse.julialang.org/t/does-functionwrappers-jl-support-parametric-polymorphism/66408/3 "2021-08-14T23:41:57Z")

</div>

A struct wrapper on top of `FunctionWrapper` with a calling method takes care of it. This is probably my primary use case of the package, actually.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 8, 2022, 7:54pm UTC](https://discourse.julialang.org/t/does-functionwrappers-jl-support-parametric-polymorphism/66408/4 "2022-07-08T19:54:35Z")

</div>

> **[GitHub - SciML/FunctionWrappersWrappers.jl](https://github.com/SciML/FunctionWrappersWrappers.jl)**
>
> Contribute to SciML/FunctionWrappersWrappers.jl development by creating an account on GitHub.
