Created
March 4, 2019 15:23
-
-
Save Ghedeon/1fc51412683663fac2c52b4a25da1165 to your computer and use it in GitHub Desktop.
NotNull delegate with lazy message
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
fun <T: Any> notNull(lazyMessage: () -> Any): ReadWriteProperty<Any?, T> = NotNullVar(lazyMessage) | |
private class NotNullVar<T: Any>(val lazyMessage: () -> Any) : ReadWriteProperty<Any?, T> { | |
private var value: T? = null | |
override fun getValue(thisRef: Any?, property: KProperty<*>): T { | |
return value ?: throw IllegalStateException(lazyMessage.toString()) | |
} | |
override 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