Why is a <:Any
constraint equivalent to there being no constraint?
For abstract types:
julia> abstract type A{T <: Any} end
julia> A{2} # Succeeds even though 2 doesn't subtype Any
A{2}
For concrete types:
julia> struct S{T <: Any} end
julia> S{2} # Succeeds even though 2 doesn't subtype Any
S{2}
For methods:
julia> f(::Val{T}) where {T <: Any} = nothing
f (generic function with 1 method)
julia> f(Val{2}()) # Succeeds even though 2 doesn't subtype Any
The above behavior seems to be inconsistent with how subtyping constraints work for all other types except for Any
, a constraint A <: B
is only valid if A
subtypes B
, except if B
is Any
. For example:
julia> struct R{T <: Number} end
julia> R{2}
ERROR: TypeError: in R, in T, expected T<:Number, got a value of type Int64
The operator <:
, in expressions, doesn’t exhibit this inconsistency: 2 <: Any
fails just the same as 2 <: Number
:
julia> 2 <: Any
ERROR: TypeError: in <:, expected Type, got a value of type Int64
julia> 2 <: Number
ERROR: TypeError: in <:, expected Type, got a value of type Int64
EDIT: fixed source code formatting