Does anybody know a better way to listen for member change within own base class?
class FOO {
val t: String
}
class BOO: FOO {
fun x(val t: String) {
// notify when t was changed
}
}
In my opinion JavaRx with Observer would be to much. Kotlin Delegate does not work with inheritance (or i could not find a way yet). The best i came up with is to override setter and getter of "t" in "BOO". But this is a little bit awkward because of this "Why should i overwrite a member?" or "Why do i have to define set and get when i just need set?"
So my solution so far:
import kotlin.properties.Delegates
fun main(args: Array<String>) {
var foo = FOO()
foo.t = "foo"
foo.printT()
var boo = BOO()
boo.t = "boo"
boo.printT()
}
open class FOO () {
open var t: String? = "origin"
set(value) {
println("\t origin setter is called")
field = String() + value // create a copy (original it is not a String :D)
}
get() {
println("\t origin getter is called")
return field
}
fun printT() = println(t)
}
class BOO (): FOO () {
override var t: String?
set(value) {
// thats all what i need
println("\t overwritten setter is called")
super.t = value // execute origin setter
}
get() {
println("\t overwritten getter is called")
return super.t
}
}