No method matching ::Type{A{<:Real}}, only ::Type{A{T}} where {T<:Real}

I can’t find this in the julia manual right now, but I’d always assumed that using <:Real inside a type on the left-hand side of a function definition f(x::A{<:Real}) is just an alias to f(x::A{T}) where {T<:Real}. But with julia 1.5.3 I’m getting a method error when trying to use ::Type{A{<:Real}}.

julia> struct A{T}; x::T; end

julia> f(::Type{A{<:Real}}) = true
f (generic function with 1 method)

julia> f(x::A) = f(typeof(x))
f (generic function with 2 methods)

julia> f(A(1))
ERROR: MethodError: no method matching f(::Type{A{Int64}})
Closest candidates are:
  f(::Type{A{var"#s1"} where var"#s1"<:Real}) at REPL[2]:1
  f(::A) at REPL[3]:1
 [1] f(::A{Int64}) at ./REPL[3]:1
 [2] top-level scope at REPL[4]:1

julia> f(::Type{A{T}}) where T = 123
f (generic function with 3 methods)

julia> f(A(1))

julia> f(::Type{A{T}}) where {T<:Real} = 456
f (generic function with 4 methods)

julia> f(A(1))


Is there a reason for this? What am I doing wrong?

You should have written

Because A{<:Real} is an UnionAll which is a form of abstract type.
And A{Int} is a subtype of it.
Not equal to it

1 Like

I get it now, thanks!