https://kotlinlang.org logo
Title
e

eekboom

10/22/2019, 10:04 AM
Consider this: Interface with a property. Class implements the interface using primary constructor with a parameter that overrides the property. Nice and easy. Now: How can I transform the argument value before that? There sure must be a better way than this:
interface Foo { val text: String }

class Bar (private var text2: String) : Foo {
    init {
        text2 = text2.replace('a', 'z')
    }
    override val text: String
        get() = text2
}
k

karelpeeters

10/22/2019, 10:42 AM
Use a constructor parameter instead of an immediate property declaration:
class Bar(text: String): Foo {
    override val text = text.replace('a', 'z')
}
e

eekboom

10/22/2019, 10:43 AM
Doh. I was sure that there’s a simple solution. :embarrased: Thanks!