https://kotlinlang.org logo
Title
l

leonardootto

08/21/2017, 1:32 PM
I need change it to more clear. Do you have some good examples of property extension?
k

karelpeeters

08/21/2017, 1:35 PM
I recently did this:
inline val Long.sign
    get() = when {
        this > 0L -> 1
        this < 0L -> -1
        else -> 0
    }

inline val Long.abs
    get() = Math.abs(this)
Turns out something like this was added in the latest version too, but I haven't updated yet.
I don't think I've come across a use for extension `var`s yet.
l

leonardootto

08/21/2017, 1:47 PM
Good. Thanks Karel. I try find some example using property extension with var. Maybe i will back to StringBuffer property extension, is more simple to understand.
🙂
b

benleggiero

08/21/2017, 1:50 PM
@karelpeeters I use them for specialized dictionaries:
inline var Map<String, MyKey>.isStarted: Boolean
    get() = this["isStarted"].booleanValue
    set(newValue: Boolean) = this["isStarted"] = MyKey(booleanValue = newValue)