Repository navigation
More specific equals operator
#476
Replies: 6 comments 11 replies
|
This looks great, I've hit this several times and it's always annoyingly hard to spot. My only question is around the "like overriding but not actually overriding" behavior:
The first one sounds a lot like the restrictions on overriding a method. This is especially true since type-safe equals operators do essentially override any from their supertypes, just without the Why is |
|
The idea of "it's an override but it can restrict the parameter it wants to accept" make me think of https://youtrack.jetbrains.com/issue/KT-79654/Receiver-bound-type-parameters. They are different features with different use-cases, but they still feel similar somehow: in both cases, the more generic call is mathematically legal, but the compiler should be able to know it's pointless because it operates on vacuous truth. |
|
Quick update: implementation is in progress. |
|
After the latest update of the proposal (c30de3d), it looks like the feature on the declaration site really boils down to adding If we take the existing code: class A {
override fun equals(other: Any?): Boolean {...}
}To support more specific equals, we will need to do the following (change class A {
- override fun equals(other: Any?): Boolean {...}
+ operator fun equals(other: @Bound(A::class) Any?): Boolean {...}
}Why not just add class A {
- override fun equals(other: Any?): Boolean {...}
+ override fun equals(other: @Bound(A::class) Any?): Boolean {...}
}Sorry if I missed something in the document, but it looks like this "more specific equals" is now just an additional contract for the override. And so it feels that If there is some reason for this to be an |
|
BTW, now that we have |
|
We have decided to split this feature in two phases. There are two reasons: first, it helps us deliver and test the feature step by step; second, there are some small design problems in the custom equality bound front we want to iron out. In 2.5.0 we'll ship "strict equality for structural classes", which roughly means that you'll get warnings for "wrong" comparisons when using data classes, data objects, final value classes, and enumeration classes. For those cases we know that the compiler-generated |
Uh oh!
There was an error while loading. Please reload this page.
This is the discussion related to the more specific
equalsoperator proposal. The current full text of the proposal can be found here.We propose a way to override the
equalsfunction fromAnywith a more specific type, which in turn unlock additional use site diagnostics. This proposal respects the current compilation strategy forequals.All reactions