https://github.com/JuliaLang/julia/pull/23117
Can someone tell what this change brings?
Can you show me an example what it enabled?
Thanks
https://github.com/JuliaLang/julia/pull/23117
Can someone tell what this change brings?
Can you show me an example what it enabled?
Thanks
In Julia v0.6, this doesn’t work:
julia> f(x::Union{Symbol, Vector{T}}) where {T} = "hello"
f (generic function with 1 method)
julia> f(:foo)
ERROR: MethodError: no method matching f(::Symbol)
Closest candidates are:
f(::Union{Array{T,1}, Symbol}) where T at REPL[1]:1
because Julia v0.6 required that the parameter T
must be defined by the type of the function arguments (in this case, x::Symbol
). . When you pass a Symbol
as x
, though, T
is not defined and the method doesn’t match, even though it should work just fine for a Symbol
argument.
In Julia v0.7 this requirement is removed:
julia> f(x::Union{Symbol, Vector{T}}) where {T} = "hello"
f (generic function with 1 method)
julia> f(:foo)
"hello"
and the function will work, as long as you don’t actually use the value of T
(since it’s still not defined when x
is a Symbol
).
FWIW I’ve run into this limitation on a real project. See Understanding the use of Union with parametric types for a brief discussion. I didn’t know the issue had been addressed. That’s cool!