littlelightcz
03/22/2019, 5:57 PM@IWantNPEToBeThrownHere
π. Or perhaps the compiler could have a flag to turn this behaviour on.Dominaezzz
03/22/2019, 6:03 PMjw
03/22/2019, 6:05 PM!!
already implied you want an NPE to be thrown. That's literally the only behavior the operator provides.ian.shaun.thomas
03/22/2019, 6:10 PMDico
03/22/2019, 6:12 PMian.shaun.thomas
03/22/2019, 6:13 PMDico
03/22/2019, 6:17 PMian.shaun.thomas
03/22/2019, 6:19 PMDico
03/22/2019, 6:23 PMorangy
littlelightcz
03/23/2019, 8:04 AMcbruegg
03/23/2019, 8:10 AM!!
operator is very important. True, you can use ?
almost everywhere, but often you don't want your code to fail silently. When you expect something to be not null at some point, it's reasonable to use !!
.littlelightcz
03/23/2019, 8:12 AMlittlelightcz
03/23/2019, 8:16 AMlittlelightcz
03/23/2019, 9:06 AMcheckNotNull()
, but instead it would also print the checked variable name in the stack trace, so it would be clear from the first sight what has been null. It could be named e.g. .notNull()
and could be used as:
val unsafeString: String? = "Shouldn't be null, but I what if I am wrong?"
println("Length is: ${unsafeString.notNull().length()}")
And in case the unsafeString
would be null sometimes, the exception would be thrown in a fashion: IllegalStateException: The 'unsafeString' was expected to be not-null.
littlelightcz
03/23/2019, 9:12 AM