You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Beyond just "nice to have," this commonly surfaces as an unexpected method sorting because it means ::MyType{R} where {R} is less specific than just ::MyType. Cf. #6383 (comment) (but this is now the more relevant issue):
julia> struct A{T <:Real}; end
julia> f(::A{T}) where {T} = T
f (generic function with 1 method)
julia> f(::A, args...) ="surprise"
f (generic function with 2 methods)
julia> f(A{Int}())
"surprise"
julia> struct B{T}; end
julia> f(::B{T}) where {T} = T
f (generic function with 3 methods)
julia> f(::B, args...) ="no surprise"
f (generic function with 4 methods)
julia> f(B{Int}())
Int64
julia> methods(f)
# 4 methods for generic function "f" from Main:
[1] f(::B{T}) where T
@ REPL[6]:1
[2] f(::B, args...)
@ REPL[7]:1
[3] f(::A, args...)
@ REPL[3]:1
[4] f(::A{T}) where T
@ REPL[2]:1
Firstly, I'd like to note that this isn't specific to UnionAll, it also applies to struct, mutable struct, and abstract type.
Secondly, perhaps we just need better documentation, as asked for in #53380? The fact is, implementing this suggestion would make the language slightly less expressive, or it could risk introducing other gotchas.
One problem is that a solution to this issue would necessarily require some special-casing, because typeintersect is not in general exact.
Consider the following definition of a parametric type named
MyType
:Now, we get
but
Because the parameter
R
ofMyType
must be a subtype ofReal
by definition, it will be nice if the last code returnstrue
.The text was updated successfully, but these errors were encountered: