Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created December 7, 2018 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganlinn/bb359a4e2fef55c3b4a9e5aeb17e1610 to your computer and use it in GitHub Desktop.
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
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