Skip to content

Commit f40832d

Browse files
committed
Use three-valued logic with missing values in ==(::Tuple, ::Tuple)
For consistency with AbstractArray.
1 parent 1046736 commit f40832d

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

base/tuple.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,16 @@ function ==(t1::Tuple, t2::Tuple)
268268
if length(t1) != length(t2)
269269
return false
270270
end
271+
anymissing = false
271272
for i = 1:length(t1)
272-
if !(t1[i] == t2[i])
273-
return false
274-
end
273+
eq = (t1[i] == t2[i])
274+
if ismissing(eq)
275+
anymissing = true
276+
elseif !eq
277+
return false
278+
end
275279
end
276-
return true
280+
return anymissing ? missing : true
277281
end
278282

279283
const tuplehash_seed = UInt === UInt64 ? 0x77cfa1eef01bca90 : 0xf01bca90

test/missing.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ end
222222
@test Union{Int, Missing}[1] != Union{Int, Missing}[2]
223223
end
224224

225+
@testset "== and != on tuples" begin
226+
@test ismissing((1, missing) == (1, missing))
227+
@test ismissing(("a", missing) == ("a", missing))
228+
@test ismissing((missing,) == (missing,))
229+
@test ismissing((missing, 2) == (1, missing))
230+
231+
@test ismissing((1, missing) != (1, missing))
232+
@test ismissing(("a", missing) != ("a", missing))
233+
@test ismissing((missing,) != (missing,))
234+
@test ismissing((missing, 2) != (1, missing))
235+
end
236+
225237
@testset "any & all" begin
226238
@test any([true, missing])
227239
@test any(x -> x == 1, [1, missing])

0 commit comments

Comments
 (0)