Created
October 22, 2017 23:30
-
-
Save felipecsl/46f69942d57a9381dea60d4f361458a4 to your computer and use it in GitHub Desktop.
A Kotlin lazy that can be set to override the initializer value
This file contains 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
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
Use it like