Created
December 7, 2018 22:42
-
-
Save loganlinn/bb359a4e2fef55c3b4a9e5aeb17e1610 to your computer and use it in GitHub Desktop.
Kotlin delegate property that allows map key to be different than property name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.properties.ReadWriteProperty | |
import kotlin.reflect.KProperty | |
fun <V> map(props: Props, key: String): ReadWriteProperty<Any?, V> { | |
return object : ReadWriteProperty<Any?, V> { | |
@Suppress("UNCHECKED_CAST") | |
override fun getValue(thisRef: Any?, property: KProperty<*>): V = props[key]!! as V | |
override fun setValue(thisRef: Any?, property: KProperty<*>, value: V) { | |
props[key] = value | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment