https://kotlinlang.org logo
#announcements
Title
# announcements
u

ursus

11/30/2019, 7:49 PM
Copy code
fun Message.diffUtilEquals(o: Any): Boolean {
    if (o !is Message) return false
    if (id != o.id) return false
    return true
}

fun TextMessage.diffUtilEquals(o: Any): Boolean {
    if (!super.diffUtilEquals(o)) return false
    if (o !is TextMessage) return false
    if (text != o.text) return false
    return true
}
n

nwh

11/30/2019, 7:52 PM
Is there a reason you're using extension functions instead of normal inheritance? If you own both the
Message
and
TextMessage
classes then why not use a good old fashioned
open fun
and override it
👍🏻 1
m

molikuner

11/30/2019, 8:03 PM
There is no way of overwriting an extension function, as they are statically resolved.
Copy code
open class A
class B : A()

fun A.x() = println("A")
fun B.x() = println("B")

val a: A = B()
a.x()
This prints
A
instead of
B
.
u

ursus

11/30/2019, 8:22 PM
@nwh because its domain layer model and diffutil is ui layer, so I didnt want dirty it (nor create ui layer model)
m

molikuner

11/30/2019, 8:31 PM
https://pl.kotl.in/amql0ZACw May be the only option, if you really need it, but I would prefer writing an extra ui layer model instead of this!
u

ursus

11/30/2019, 9:05 PM
@molikuner yea I had something like that in mind, however the overriding was supposed to share code from Message, as my Message has id, and some other fields. Yes I can workaround it via baseDiffEquals and call it manually, but then if I were to have some more inheritance, Id need to spell out all the super classes
bummer
matter of fact I do, since example of hierachy is TextMessage : ContentMessage : Message. so I need to
Copy code
fun TextMessage.diffEquals {
   messageDiffEquals
   contentMessageDiffEquals
   my fields ..
}
right?
2 Views