Skip to content

Type{T} where T as function arguments sometimes do not invoke "method ambiguity" error #58644

@frankwswang

Description

@frankwswang

MWE (tested on Julia 1.11.5):

  • This will not invoke the error:
julia> foo1(::Type{<:Real}, ::Type{<:Real}) = false
foo1 (generic function with 1 method)

julia> foo1(::Type{<:AbstractFloat}, ::Type{<:AbstractFloat}) = true
foo1 (generic function with 2 methods)

julia> foo1(::Type{Union{}}, ::Type{<:Real}) = 1
foo1 (generic function with 3 methods)

julia> foo1(Union{}, Float32)
1
  • This will not invoke the error either:
julia> foo2(::Type{T1}, ::Type{T2}) where {T1<:Real, T2<:Real} = false
foo2 (generic function with 1 method)

julia> foo2(::Type{T1}, ::Type{T2}) where {T1<:AbstractFloat, T2<:AbstractFloat} = true
foo2 (generic function with 2 methods)

julia> foo2(::Type{Union{}}, ::Type{T2}) where {T2<:Real} = 1
foo2 (generic function with 3 methods)

julia> foo2(Union{}, Float32)
1
  • This will invoke the error:
julia> foo3(::Type{T1}, ::Type{T2}) where {T1<:Real, T2<:Real} = false
foo3 (generic function with 1 method)

julia> foo3(::Type{T1}, ::Type{T2}) where {T1<:AbstractFloat, T2<:AbstractFloat} = true
foo3 (generic function with 2 methods)

julia> foo3(::Type{T1}, ::Type{T2}) where {T1<:Union{}, T2<:Real} = 1
foo3 (generic function with 3 methods)

julia> foo3(Union{}, Float32)
ERROR: MethodError: foo3(::Type{Union{}}, ::Type{Float32}) is ambiguous.

Metadata

Metadata

Assignees

Labels

types and dispatchTypes, subtyping and method dispatch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions