Skip to content

Instantly share code, notes, and snippets.

@Ghedeon
Created March 4, 2019 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ghedeon/1fc51412683663fac2c52b4a25da1165 to your computer and use it in GitHub Desktop.
Save Ghedeon/1fc51412683663fac2c52b4a25da1165 to your computer and use it in GitHub Desktop.
NotNull delegate with lazy message
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