Skip to content

Instantly share code, notes, and snippets.

@suclike
Forked from felipecsl/MutableLazy.kt
Created June 15, 2018 15:31
Show Gist options
  • Select an option

  • Save suclike/c177d73702722d2abfefa4e3d7de3f5e to your computer and use it in GitHub Desktop.

Select an option

Save suclike/c177d73702722d2abfefa4e3d7de3f5e to your computer and use it in GitHub Desktop.
A Kotlin lazy that can be set to override the initializer value
private fun <T> mutableLazy(initializer: () -> T) = Delegate(lazy(initializer))
class Delegate<T>(private val lazy: Lazy<T>) {
private var value: T? = null
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
return value ?: lazy.getValue(thisRef, property)
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
this.value = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment