Skip to content

Commit

Permalink
Define ==/isequal/hash for Some by forwarding to the wrapped …
Browse files Browse the repository at this point in the history
…value (#52421)

The equality of two `Some` depends on the equality of the wrapped 
value, once the `Some` is unwrapped.
---------

Co-authored-by: Alex Arslan <ararslan@comcast.net>
Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
  • Loading branch information
3 people authored May 10, 2024
1 parent 4793328 commit a910a99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,8 @@ macro something(args...)
end
return expr
end

==(a::Some, b::Some) = a.value == b.value
isequal(a::Some, b::Some)::Bool = isequal(a.value, b.value)
const hash_some_seed = UInt == UInt64 ? 0xde5c997007a4ca3a : 0x78c29c09
hash(s::Some, h::UInt) = hash(s.value, hash_some_seed + h)
26 changes: 26 additions & 0 deletions test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@
@test !isequal(Some(1), nothing)
@test !isequal(Some(nothing), nothing)

# Some with something else is false
@test !=(Some(nothing), nothing)
@test !=(nothing, Some(nothing))

# Two `Some`s forward to their wrapped things
@test ==(Some([0x1]), Some([1]))

# propagate wrapped missings
@test !=(Some(1), Some(missing)) isa Missing
@test !=(Some(missing), Some(1)) isa Missing
@test ==(Some(missing), Some(missing)) isa Missing

# Make sure to still propagate non-wrapped Missing
@test ==(Some(1), missing) isa Missing
@test ==(missing, Some(1)) isa Missing

@test isequal(Some([0x1]), Some([1]))
@test !isequal(missing, Some(missing))
@test !isequal(Some(missing), missing)
@test isequal(Some(missing), Some(missing))

# hashing implications
@test hash(Some(0x1)) != hash(0x1)
@test hash(Some(0x1)) == hash(Some(1))
@test hash((Some(1),)) != hash((1, Some))

@testset "something" begin
@test_throws ArgumentError something()
@test something(1) === 1
Expand Down

0 comments on commit a910a99

Please sign in to comment.