Sealed sub-class -> object: handle equals, suggest "add equals"
Before this commit, sealed sub-class without state was considered a style issue. After this commit, sealed sub-class without state AND custom equals is considered a probable bug, because comparison of its instances is very fragile. Alternative fix (generate equals & hashCode by identity) is added.
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
// FIX: Generate equals & hashCode by identity
|
||||
|
||||
abstract class Base {
|
||||
open val prop: Int
|
||||
get() = 13
|
||||
}
|
||||
|
||||
sealed class SC : Base() {
|
||||
class U : SC() {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return this === other
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return System.identityHashCode(this)
|
||||
}
|
||||
}
|
||||
|
||||
override val prop: Int
|
||||
get() = 42
|
||||
}
|
||||
Reference in New Issue
Block a user