Added equals/hashCode contract to Vector2D/Vector3D #482

Closed
whiskeysierra wants to merge 1 commits from master into master
whiskeysierra commented 2022-04-19 15:14:10 +03:00 (Migrated from github.com)

This changes add an explicit equals+ hashCode contract to both Vector2D and Vector3D.
Before this change, if one were to implement the Vector3D interface directly, it would violate the the symmetric and transitive aspects of Object/Any's equals contract.

A typical use case where one would implement the interface directly (compared to just using the provided data class) is to create a more natural adapter from some internal code base or other libraries to this one.
Think of an imaginary data type MyVector which has also represents a 3D vector using x, y and z components. By changing MyVector to implement Vector3D, I immediately get access to all sorts of nice features from this library: extension functions, operators, rotations, etc.

This changes add an explicit `equals`+ `hashCode` contract to both `Vector2D` and `Vector3D`. Before this change, if one were to implement the `Vector3D` interface directly, it would violate the the *symmetric* and *transitive* aspects of `Object`/`Any`'s `equals` contract. A typical use case where one would implement the interface directly (compared to just using the provided data class) is to create a more natural adapter from some internal code base or other libraries to this one. Think of an imaginary data type `MyVector` which has also represents a 3D vector using x, y and z components. By changing `MyVector` to implement `Vector3D`, I immediately get access to all sorts of nice features from this library: extension functions, operators, rotations, etc.
altavir commented 2022-04-26 15:11:52 +03:00 (Migrated from github.com)

Comparing real-valued vectors seems to be a bad thing. You need to add a tolerance for any floating point number comarison. Also I would like to keep Vectors and other algebra elements as identity-free value-classes.

I would like to suggest introducing an algebra-based comparion like EuclidianSpace2D.contentEquals(v1: Vector2D, v2 : Vector2D, tolerance: Double): Boolean.

Comparing real-valued vectors seems to be a bad thing. You need to add a tolerance for any floating point number comarison. Also I would like to keep Vectors and other algebra elements as identity-free value-classes. I would like to suggest introducing an algebra-based comparion like `EuclidianSpace2D.contentEquals(v1: Vector2D, v2 : Vector2D, tolerance: Double): Boolean`.
altavir commented 2022-04-26 15:14:11 +03:00 (Migrated from github.com)

See discussion in #470 and #248

See discussion in #470 and #248
altavir commented 2022-05-04 17:45:41 +03:00 (Migrated from github.com)

Closing because of discussion above. Adding algebra-level methods would be appreciated.

Closing because of discussion above. Adding algebra-level methods would be appreciated.

Pull request closed

Sign in to join this conversation.
No description provided.