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")
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> 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
2 Likes
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.
1 Like