4
\$\begingroup\$

I am a Java Android Developer and I'm approaching Kotlin.

I have defined the following class:

open class Player : RealmObject() {
 ...
}

And I 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 I want is if I 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 I wrote up there is actually working as expected :D

Is this the good way? Or there is some better way?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Aug 3, 2017 at 16:18
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Please correct me if I got you wrong, but we're talking here about simple inheritance:

open class RealmObject {
 fun store() { /*RealmObject impl*/ } 
}
open class Player : RealmObject() {
 override fun store() { 
 /*Player impl*/ 
 super.store() /*RealmObject impl*/
 }
 ...
}

Your wanted behaviour fits:

Player().store() // executes override function - Player impl, then RealmObject impl
RealmObject().store() // executes RealmObject implementation

If you want to keep it as extension functions, your code is valid, but isn't really an eye-candy.

Both classes Player and RealmObject would have the same function store(), but they are 'unrelated' - thats why we don't see any override. Extension functions extend statically and they can shadow your implementation, which makes your code very hard to read and the behaviour becomes at some point not understandable.

Please use extension functions wisely and try to never use it for inheritance.

answered Oct 4, 2019 at 8:18
\$\endgroup\$
0
\$\begingroup\$

It's not a direct answer to your technical question, which you seem to have answered.

I'm not sure that the Player.loggedAt field should be set when store() is called. We only see a very small fraction of your app, but maybe that could be set somewhere else more naturally. From the name, it seems maybe it should be set when the Player is created, or recreated from the DB.

answered Aug 8, 2017 at 19:27
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.