Skip to content

Commit

Permalink
Use three-valued logic with missing values in ==(::Tuple, ::Tuple)
Browse files Browse the repository at this point in the history
For consistency with AbstractArray.
  • Loading branch information
nalimilan committed Dec 7, 2017
1 parent 1046736 commit f40832d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,16 @@ function ==(t1::Tuple, t2::Tuple)
if length(t1) != length(t2)
return false
end
anymissing = false
for i = 1:length(t1)
if !(t1[i] == t2[i])
return false
end
eq = (t1[i] == t2[i])
if ismissing(eq)
anymissing = true
elseif !eq
return false
end
end
return true
return anymissing ? missing : true
end

const tuplehash_seed = UInt === UInt64 ? 0x77cfa1eef01bca90 : 0xf01bca90
Expand Down
12 changes: 12 additions & 0 deletions test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ end
@test Union{Int, Missing}[1] != Union{Int, Missing}[2]
end

@testset "== and != on tuples" begin
@test ismissing((1, missing) == (1, missing))
@test ismissing(("a", missing) == ("a", missing))
@test ismissing((missing,) == (missing,))
@test ismissing((missing, 2) == (1, missing))

@test ismissing((1, missing) != (1, missing))
@test ismissing(("a", missing) != ("a", missing))
@test ismissing((missing,) != (missing,))
@test ismissing((missing, 2) != (1, missing))
end

@testset "any & all" begin
@test any([true, missing])
@test any(x -> x == 1, [1, missing])
Expand Down

0 comments on commit f40832d

Please sign in to comment.