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

Pointer equality for Slice #14720

Open
straight-shoota opened this issue Jun 15, 2024 · 4 comments · May be fixed by #14728
Open

Pointer equality for Slice #14720

straight-shoota opened this issue Jun 15, 2024 · 4 comments · May be fixed by #14728

Comments

@straight-shoota
Copy link
Member

straight-shoota commented Jun 15, 2024

Slice implements the equality operator == as structural equality, i.e. two instances are considered equal if they have equivalent content.
Two instances are considered equal if they have the same values: Slice[1, 2, 3] == Slice[1, 2, 3] (or even just values expressing the same concept in different types: Slice[1.0, 2.0, 3.0] == Slice[1, 2, 3]).

There is no method for referential equality, i.e. if two instances pointing to the same memory. Slice is a struct type but it wraps a pointer, so it has semantics similar to a reference type.
Referential equality would be similar to equality of the pointers, but checking size as well - Slice is a pointer plus size.

Usually this is implemented as #same? and I think this would work well for Slice.

slice = Slice[1, 2, 3]
slice.same?(slice)           # => true
slice.same? Slice[1, 2, 3]   # => false
slice.same?(slice + 1)       # => false
(slice + 1).same?(slice + 1) # => true
slice.same?(slice[0, 2])     # => false

Related to referential equality and the implementation of same? is also #object_id. For example Set is a struct type but behaves like a reference because it's a wrapper of an reference type. It implements both #same? and #object_id (which just delegate to the wrapped reference). This doesn't work for Slice because the wrapped pointer is not the only defining characteristic. Size is relevant as well.
So I don't think #object_id makes sense for Slice because the pointer address is not unique. Two Slice instances can share the same pointer address, but with different sizes they would still be referentially unequal.

@ysbaddaden
Copy link
Contributor

ysbaddaden commented Jun 17, 2024

I think this example should be true instead of false?

slice == Slice[1, 2, 3]      # => false

@straight-shoota
Copy link
Member Author

straight-shoota commented Jun 17, 2024

No, it's false. Slice[1, 2, 3] allocates new memory for its contents. So it has a different pointer than slice.

Doh 🤦 It should be same? instead of ==.
Fixed.

@straight-shoota straight-shoota linked a pull request Jun 18, 2024 that will close this issue
@HertzDevil
Copy link
Contributor

IIUC this alone won't let you use Slices in a compare-by-identity Hash yet, right?

@straight-shoota
Copy link
Member Author

Correct. Compare by identity is based on object id and falls back to ==.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants