iI am a Java Android Developer and i'mI'm approaching to Kotlin.
I have defined the following class:
open class Player : RealmObject() {
...
}
And iI defined the following two extensions, one for the generic RealmObject class and one for the specific Player class:
fun RealmObject.store() {
Realm.getDefaultInstance().use { realm ->
realm.beginTransaction()
realm.copyToRealmOrUpdate(this)
realm.commitTransaction()
}
}
fun Player.store(){
this.loggedAt = Date()
(this as RealmObject).store()
}
What iI want is if iI call .store() on any RealmObject object, the RelamObject.store() extension will be called BUT if i call .store() on a Player instance the extension that will be called will be Player.store().
(No problem for now)
I don't want to copy paste the same code, i love to write less reuse more.
So i need that internally the Player.store() will call the generic RealmObject.store()
I got it. The code iI wrote up there is actually working as expected :D
What i am askingIs this the good way? Or there is (just because i wrote that just by personally intuition):
Is this the good way?! Or there is some better way?
Thank yousome better way?