Skip to content

Define == for Some by forwarding to the wrapped value #52421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 10, 2024
8 changes: 8 additions & 0 deletions base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,11 @@ macro something(args...)
something = GlobalRef(Base, :something)
return :($something($expr))
end

==(a::Some, b::Some) = a.value == b.value
==(::Some{Missing}, ::Some{T}) where T = T === Missing
==(::Some{T}, ::Some{Missing}) where T = T === Missing
==(::Some{Missing}, ::Some{Missing}) = true

isequal(a::Some, b::Some) = isequal(a.value, b.value)
hash(s::Some, h::UInt) = hash(s.value, hash(Some, h))
28 changes: 28 additions & 0 deletions test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@
@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 Somes forward to their wrapped things
@test ==(Some([0x1]), Some([1]))

# Don't propagate wrapped missings
@test !=(Some(1), Some(missing))
@test !=(Some(missing), Some(1))
@test ==(Some(missing), Some(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(Some(missing), Some([1]))
@test !isequal(Some(1), Some(missing))
@test isequal(Some(missing), Some(missing))

@test !isequal(missing, Some(missing))
@test !isequal(Some(missing), missing)

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

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