Skip to content
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

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

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

Seelengrab marked this conversation as resolved.
Show resolved Hide resolved
==(a::Some, b::Some)::Bool = a.value == b.value
# seperated out to ensure the above assertion works

Check warning on line 150 in base/some.jl

View workflow job for this annotation

GitHub Actions / Check for new typos

perhaps "seperated" should be "separated".
==(::Some{Missing}, ::Some) = missing
==(::Some, ::Some{Missing}) = missing
==(::Some{Missing}, ::Some{Missing}) = missing

Seelengrab marked this conversation as resolved.
Show resolved Hide resolved
isequal(a::Some, b::Some)::Bool = isequal(a.value, b.value)
hash(s::Some, h::UInt) = hash(s.value, hash(Some, h))
Seelengrab marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@
@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

Check warning on line 59 in test/some.jl

View workflow job for this annotation

GitHub Actions / Check for new typos

perhaps "missings" should be "missing".
@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))
Seelengrab marked this conversation as resolved.
Show resolved Hide resolved

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