Skip to content

Commit

Permalink
improve the inferrability of isequal(::Any, ::Any) (JuliaLang#42195)
Browse files Browse the repository at this point in the history
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:
<https://github.com/JuliaLang/julia/blob/2f00c5f6e2211ed1588976dbbe7b022148716d95/base/missing.jl#L80-L82>
, and we can improve the inferrability by type annotation.
  • Loading branch information
aviatesk authored and LilithHafner committed Mar 8, 2022
1 parent 1d406e9 commit 902b65f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 902b65f

Please sign in to comment.