From 03e1a89ff1f62f05cf3937f5da44f3d46956138a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Thu, 13 Aug 2020 16:37:29 +0200 Subject: [PATCH] Define `OrderStyle` for `Union{}` (#36810) Removes ambiguity: ``` julia> Base.OrderStyle(Union{}) ERROR: MethodError: Base.OrderStyle(::Type{Union{}}) is ambiguous. Candidates: ``` This error is relevant as with current `unique!` definition that relies on `OrderStyle` one can have a problem in corner cases. E.g.: ``` julia> [i for i in ["1"] if i isa Int] 0-element Array{Union{},1} ``` Co-authored-by: Milan Bouchet-Valat --- base/traits.jl | 1 + test/sets.jl | 3 +++ 2 files changed, 4 insertions(+) diff --git a/base/traits.jl b/base/traits.jl index b7239230686c8..53ae14b12c61e 100644 --- a/base/traits.jl +++ b/base/traits.jl @@ -11,6 +11,7 @@ OrderStyle(::Type{<:Real}) = Ordered() OrderStyle(::Type{<:AbstractString}) = Ordered() OrderStyle(::Type{Symbol}) = Ordered() OrderStyle(::Type{<:Any}) = Unordered() +OrderStyle(::Type{Union{}}) = Ordered() # trait for objects that support arithmetic abstract type ArithmeticStyle end diff --git a/test/sets.jl b/test/sets.jl index 2f08ecb210cbb..b090a3d7097fa 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -447,6 +447,9 @@ end @test @inferred(unique!(iseven, [2, 3, 5, 7, 9])) == [2, 3] @test @inferred(unique!(x -> x % 2 == 0 ? :even : :odd, [1, 2, 3, 4, 2, 2, 1])) == [1, 2] @test @inferred(unique!(x -> x % 2 == 0 ? :even : "odd", [1, 2, 3, 4, 2, 2, 1]; seen=Set{Union{Symbol,String}}())) == [1, 2] + + @test isempty(unique!(Union{}[])) + @test eltype(unique!([i for i in ["1"] if i isa Int])) <: Union{} end @testset "allunique" begin