Files
kotlin-fork/compiler/testData/codegen/box/inlineClasses/inlineClassBothEqualsOverride.kt
T
vladislav.grechko e0c8142106 Support of custom 'equals' and 'hashCode' in inline classes
'equals' from any made available for overriding in inline classes
'typed' equals made available for definition in inline classes
'typed' equals definition made compulsory if 'untyped' is overridden
'operator' keyword is allowed in 'typed' equals definition

^KT-24874: Fixed
2022-10-10 16:52:34 +00:00

55 lines
1.1 KiB
Kotlin
Vendored

// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses, +CustomEqualsInInlineClasses
// TARGET_BACKEND: JVM_IR
interface I {
fun getVal(): Int
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC1(val x: Int) : I {
override fun getVal(): Int {
return x
}
fun equals(other: IC1): Boolean {
return x == other.x
}
override fun equals(other: Any?): Boolean {
if (other !is I) {
return false
}
return getVal() == other.getVal()
}
override fun hashCode(): Int {
return getVal()
}
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class IC2(val y: Int) : I {
override fun getVal(): Int {
return y * 10
}
fun equals(other: IC2): Boolean {
return y == other.y
}
override fun equals(other: Any?): Boolean {
if (other !is I) {
return false
}
return getVal() == other.getVal()
}
override fun hashCode(): Int {
return getVal()
}
}
fun box(): String = if (setOf(IC1(10), IC2(1)).size == 1) "OK" else "Fail"