Shawn
09/21/2017, 6:04 AM==
if you just defined another operator fun equals
bdawg.io
09/21/2017, 6:07 AMjstuyts-squins
09/21/2017, 6:07 AMAny?
, because the compiler cannot enforce a Value
being passed to your own equals(...)
method:
val x: Any = Value(1)
val y: Any = Value(2)
println(x == y) // Both are "Any" here, so the compiler does not know about "Value.equals(Value)"
user
09/21/2017, 6:08 AMhttps://kotlinlang.slack.com/files/U6HSZBM3K/F76CLU44B/screen_shot_2017-09-21_at_12.07.15_am.png▾
bdawg.io
09/21/2017, 6:09 AMprintln(x + y)
bdawg.io
09/21/2017, 6:13 AMUnresolved reference. None of the following candidates is applicable because of receiver type mismatch: (lists 53 potential 'plus' methods)
^ That’s the compilation result. But if they are not explicitly typed as Any
the compiler finds Value.plus(other: Value): Value
jstuyts-squins
09/21/2017, 6:14 AMAny
) and an operator/function and tries to find that operator/function on the type it seesjstuyts-squins
09/21/2017, 6:15 AMjstuyts-squins
09/21/2017, 6:17 AMbdawg.io
09/21/2017, 6:17 AMjstuyts-squins
09/21/2017, 6:20 AMequals(Any?)
. So if you do this, the compiler will pick equals(Value)
because it is more specific:
val x = Value(1)
val y = Value(2)
println(x.equals(y))
But in Kotlin, the ==
operator is implemented using operator function equals(Any?)
, so it will not look for other functions. You can still use the same syntax as in Java though, and then it will workbdawg.io
09/21/2017, 6:20 AMjstuyts-squins
09/21/2017, 6:23 AMbdawg.io
09/21/2017, 6:24 AMequals
operator specifically is limited to an exact method signatureShawn
09/21/2017, 6:29 AM+
works now, since it seems to Shawn
09/21/2017, 6:31 AMplus
isn’t defined on Any
, so no override happening thereShawn
09/21/2017, 6:32 AM==
might be mapped specifically to Any$equals
Shawn
09/21/2017, 6:33 AMjstuyts-squins
09/21/2017, 6:35 AMplus
for Int
, etc. There is no definition of plus
on Number
, so each number type defines it separatelybdawg.io
09/21/2017, 6:38 AMequals
convention to be consistent to plus
, it would need to use the most qualified type and then follow the hierarchy thereafter until it finds a matchuser
09/21/2017, 6:40 AMhttps://kotlinlang.slack.com/files/U6HSZBM3K/F76F16X0S/screen_shot_2017-09-21_at_12.36.33_am.png▾
open
on the A.plus(other: A)
definition and the B
class, I was playing with some other stuff and intended to strip them before the screenshot (since they are irrelevant to the example)jstuyts-squins
09/21/2017, 6:47 AMequals
. Hopefully one of the JB people can tell you whybdawg.io
09/21/2017, 6:48 AM(Any?) -> Boolean
signature with the plus
conventionbdawg.io
09/21/2017, 6:50 AMjstuyts-squins
09/21/2017, 6:51 AMequals(Any?)
? You will then also be able to compare to all other objectsbdawg.io
09/21/2017, 6:55 AMplus
, minus
, etc. and even compareTo
) provide a modular way to overload the operator instead of having to share this override hierarchy that can potentially quickly build up to a large call stack with a bunch of super
handoffsbdawg.io
09/21/2017, 6:56 AMgildor
09/21/2017, 6:56 AMequals
is Java legacy and important thing for compatibility with Javabdawg.io
09/21/2017, 6:57 AMequals
methodgildor
09/21/2017, 6:59 AMValue
instead of Object
for argument type? I’m pretty sure you cannot