Skip to content

Commit 96ce2c5

Browse files
SeelengrabararslanLilithHafner
authored andcommitted
Define ==/isequal/hash for Some by forwarding to the wrapped value (JuliaLang#52421)
The equality of two `Some` depends on the equality of the wrapped value, once the `Some` is unwrapped. --------- Co-authored-by: Alex Arslan <[email protected]> Co-authored-by: Lilith Orion Hafner <[email protected]>
1 parent 6719bf6 commit 96ce2c5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

base/some.jl

+5
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,8 @@ macro something(args...)
166166
end
167167
return expr
168168
end
169+
170+
==(a::Some, b::Some) = a.value == b.value
171+
isequal(a::Some, b::Some)::Bool = isequal(a.value, b.value)
172+
const hash_some_seed = UInt == UInt64 ? 0xde5c997007a4ca3a : 0x78c29c09
173+
hash(s::Some, h::UInt) = hash(s.value, hash_some_seed + h)

test/some.jl

+26
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@
4949
@test !isequal(Some(1), nothing)
5050
@test !isequal(Some(nothing), nothing)
5151

52+
# Some with something else is false
53+
@test !=(Some(nothing), nothing)
54+
@test !=(nothing, Some(nothing))
55+
56+
# Two `Some`s forward to their wrapped things
57+
@test ==(Some([0x1]), Some([1]))
58+
59+
# propagate wrapped missings
60+
@test !=(Some(1), Some(missing)) isa Missing
61+
@test !=(Some(missing), Some(1)) isa Missing
62+
@test ==(Some(missing), Some(missing)) isa Missing
63+
64+
# Make sure to still propagate non-wrapped Missing
65+
@test ==(Some(1), missing) isa Missing
66+
@test ==(missing, Some(1)) isa Missing
67+
68+
@test isequal(Some([0x1]), Some([1]))
69+
@test !isequal(missing, Some(missing))
70+
@test !isequal(Some(missing), missing)
71+
@test isequal(Some(missing), Some(missing))
72+
73+
# hashing implications
74+
@test hash(Some(0x1)) != hash(0x1)
75+
@test hash(Some(0x1)) == hash(Some(1))
76+
@test hash((Some(1),)) != hash((1, Some))
77+
5278
@testset "something" begin
5379
@test_throws ArgumentError something()
5480
@test something(1) === 1

0 commit comments

Comments
 (0)