From c4dd759e5068afce8a4734d8eba56161c23d8b2c Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Fri, 10 Sep 2021 13:13:58 +0900 Subject: [PATCH] improve the inferrability of `isequal(::Any, ::Any)` Currently, the return type of `isequal(::Any, ::Any)` is inferred as `Union{Bool,Missing}` because it takes the possibilities of e.g. `==(::Any, ::Missing)` into account. But actually `isequal` already handles every `missing` case by dispatch: , and thus we can improve the inferrability by type annotation. --- base/operators.jl | 2 +- test/operators.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/base/operators.jl b/base/operators.jl index 74cf3e95145a6..1966ae8faf164 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -137,7 +137,7 @@ julia> isequal(missing, missing) true ``` """ -isequal(x, y) = x == y +isequal(x, y) = (x == y)::Bool # all `missing` cases are handled in missing.jl signequal(x, y) = signbit(x)::Bool == signbit(y)::Bool signless(x, y) = signbit(x)::Bool & !signbit(y)::Bool diff --git a/test/operators.jl b/test/operators.jl index d07f3382f53a5..97edebc0ea6f3 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -81,6 +81,14 @@ import Base.< @test isequal(minmax(TO23094(2), TO23094(1))[1], TO23094(1)) @test isequal(minmax(TO23094(2), TO23094(1))[2], TO23094(2)) +let m = Module() + @eval m begin + struct Foo end + foo(xs) = isequal(xs[1], Foo()) + end + @test !(@inferred m.foo(Any[42])) +end + @test isless('a','b') @testset "isgreater" begin