Created
April 26, 2022 08:27
-
-
Save vitoksmile/efe6bec246d662a1aa299972617ed16a to your computer and use it in GitHub Desktop.
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
import kotlin.properties.ReadWriteProperty | |
import kotlin.reflect.KProperty | |
/** | |
* getValue returns only once the exact value, in other cases [default] value will be returned | |
*/ | |
private class OneShotFieldDelegate<T : Any?>( | |
private val default: T, | |
) : ReadWriteProperty<Any?, T> { | |
private var value: T? = null | |
override fun getValue(thisRef: Any?, property: KProperty<*>): T { | |
val result = value ?: default | |
value = default | |
return result | |
} | |
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | |
this.value = value | |
} | |
} | |
fun <T> oneShow(default: T): ReadWriteProperty<Any, T> = OneShotFieldDelegate(default) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment