Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@felipecsl

This comment has been minimized.

Copy link
Owner Author

felipecsl commented Oct 22, 2017

Use it like

var foo: String by mutableLazy { "bar" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.