Skip to content

Commit

Permalink
Merge pull request #166 from phipsgabler/phg/equality
Browse files Browse the repository at this point in the history
Remove type parameters from == methods of IndexLens and CompositeLens
  • Loading branch information
jw3126 authored Dec 4, 2021
2 parents d8938be + 9243ef8 commit bff69d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Setfield"
uuid = "efcf1570-3423-57d1-acb7-fd33fddbac46"
version = "0.8.0"
version = "0.8.1"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
4 changes: 2 additions & 2 deletions src/lens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct ComposedLens{LO, LI} <: Lens
inner::LI
end

function ==(l1::ComposedLens{LO, LI}, l2::ComposedLens{LO, LI}) where {LO, LI}
function ==(l1::ComposedLens, l2::ComposedLens)
return l1.outer == l2.outer && l1.inner == l2.inner
end

Expand Down Expand Up @@ -193,7 +193,7 @@ struct IndexLens{I <: Tuple} <: Lens
indices::I
end

==(l1::IndexLens{I}, l2::IndexLens{I}) where {I} = l1.indices == l2.indices
==(l1::IndexLens, l2::IndexLens) = l1.indices == l2.indices

const SALT_INDEXLENS = make_salt(0x8b4fd6f97c6aeed6)
hash(l::IndexLens, h::UInt) = hash(l.indices, SALT_INDEXLENS + h)
Expand Down
32 changes: 25 additions & 7 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ end
# singletons (identity and property lens) are egal
for (l1, l2) [
@lens(_) => @lens(_),
@lens(_.a) => @lens(_.a)
@lens(_.a) => @lens(_.a),
]
@test l1 === l2
@test l1 == l2
Expand All @@ -218,23 +218,41 @@ end

# composite and index lenses are structurally equal
for (l1, l2) [
@lens(_[1]) => @lens(_[1])
@lens(_.a[2]) => @lens(_.a[2])
@lens(_.a.b[3]) => @lens(_.a.b[3])
@lens(_[1]) => @lens(_[1]),
@lens(_.a[2]) => @lens(_.a[2]),
@lens(_.a.b[3]) => @lens(_.a.b[3]),
@lens(_[1:10]) => @lens(_[1:10]),
@lens(_.a[2:20]) => @lens(_.a[2:20]),
@lens(_.a.b[3:30]) => @lens(_.a.b[3:30]),
]
@test l1 == l2
@test hash(l1) == hash(l2)
end

# inequality
for (l1, l2) [
@lens(_[1]) => @lens(_[2])
@lens(_.a[1]) => @lens(_.a[2])
@lens(_.a[1]) => @lens(_.b[1])
@lens(_[1]) => @lens(_[2]),
@lens(_.a[1]) => @lens(_.a[2]),
@lens(_.a[1]) => @lens(_.b[1]),
@lens(_[1:10]) => @lens(_[2:20]),
@lens(_.a[1:10]) => @lens(_.a[2:20]),
@lens(_.a[1:10]) => @lens(_.b[1:10]),
]
@test l1 != l2
end

# equality with non-equal range types (#165)
for (l1, l2) [
@lens(_[1:10]) => @lens(_[Base.OneTo(10)]),
@lens(_.a[1:10]) => @lens(_.a[Base.OneTo(10)]),
@lens(_.a.b[1:10]) => @lens(_.a.b[Base.OneTo(10)]),
@lens(_.a[Base.StepRange(1, 1, 5)].b[1:10]) => @lens(_.a[1:5].b[Base.OneTo(10)]),
@lens(_.a.b[1:3]) => @lens(_.a.b[[1, 2, 3]]),
]
@test l1 == l2
@test hash(l1) == hash(l2)
end

# Hash property: equality implies equal hashes, or in other terms:
# lenses either have equal hashes or are unequal
# Because collisions can occur theoretically (though unlikely), this is a property test,
Expand Down

2 comments on commit bff69d5

@jw3126
Copy link
Owner Author

@jw3126 jw3126 commented on bff69d5 Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/49928

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.1 -m "<description of version>" bff69d586a072e0b3342415c0bdf39f935ff321b
git push origin v0.8.1

Please sign in to comment.