iex
05/07/2020, 3:05 PMthana
05/07/2020, 3:09 PMorElseThrow
?rax
05/07/2020, 3:10 PMmyOptional ?: throw Error("myOptional")
iex
05/07/2020, 3:10 PMiex
05/07/2020, 3:10 PMfun <T> T?.unwrap(errorMsg: String): T =
this ?: error(errorMsg)
iex
05/07/2020, 3:10 PMiex
05/07/2020, 3:11 PMinputs.cough?.unwrap("Cough not set").type = type // error after ), because it's missing ?
iex
05/07/2020, 3:11 PM(inputs.cough ?: error("Cough not set")).type = type
iex
05/07/2020, 3:11 PMthana
05/07/2020, 3:13 PMthana
05/07/2020, 3:14 PMrequireNotNull
already exists in kotlinrax
05/07/2020, 3:29 PM@ExperimentalContracts
fun <T> T.unwrap(errorMessage: String): T {
contract {
returns()
}
return this ?: throw Error(errorMessage)
}
Usage:
val a: String?
a = null
a?.unwrap("a is Null!!")
a // a would smart cast to not-null here.
rax
05/07/2020, 3:31 PMrequireNotNull
, checkNotNull
@thana recommended.thana
05/07/2020, 3:33 PM!!
if you don't care for the concrete error 🙈rax
05/07/2020, 3:34 PMiex
05/07/2020, 3:41 PMiex
05/07/2020, 3:42 PM!!
😉 I wanted an error messageiex
05/07/2020, 3:42 PM