From 30efb9a4dd0c048549e6e986c12f12b2b85d82f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Mon, 27 Jul 2020 00:01:58 +0200 Subject: [PATCH 1/4] Define `OrderStyle` for `Union{}` 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} ``` I propose to make this case `Unordered()` as probably this was the intention of the original implementation, but probably it does not matter much which option is chosen. --- base/traits.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/traits.jl b/base/traits.jl index b7239230686c8..7913a2f1c0fbe 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{}}) = Unordered() # trait for objects that support arithmetic abstract type ArithmeticStyle end From 2d704ae332f289979de15c5c92563f57d39d60f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Mon, 27 Jul 2020 00:07:12 +0200 Subject: [PATCH 2/4] Add test for unique! on Union{}[] --- test/sets.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/sets.jl b/test/sets.jl index 3c213b5426c9d..59c765e2dcd6f 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -440,6 +440,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 From f42eb32e442ff2227d70aafc7b192c9c70d6f80f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Mon, 27 Jul 2020 00:34:09 +0200 Subject: [PATCH 3/4] make Union{} Ordered --- base/traits.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/traits.jl b/base/traits.jl index 7913a2f1c0fbe..53ae14b12c61e 100644 --- a/base/traits.jl +++ b/base/traits.jl @@ -11,7 +11,7 @@ OrderStyle(::Type{<:Real}) = Ordered() OrderStyle(::Type{<:AbstractString}) = Ordered() OrderStyle(::Type{Symbol}) = Ordered() OrderStyle(::Type{<:Any}) = Unordered() -OrderStyle(::Type{Union{}}) = Unordered() +OrderStyle(::Type{Union{}}) = Ordered() # trait for objects that support arithmetic abstract type ArithmeticStyle end From 08464916108456867b292f556696a017e21fd370 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Mon, 27 Jul 2020 15:57:09 +0200 Subject: [PATCH 4/4] Remove whitespace --- test/sets.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sets.jl b/test/sets.jl index 59c765e2dcd6f..845415ec131a2 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -440,7 +440,7 @@ 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