val hasMyPredicate = someObject.someField.hisField.finalField != null
val someOtherBoolean = extensiveFunction()
when{
hasMyPredicate && someOtherBoolean -> {...}
!hasMyPredicate && someOtherBoolean -> {...}
!hasMyPredicate && !someOtherBoolean -> {...}
hasMyPredicate && !someOtherBoolean -> { val value = someObject.someField.hisField.finalField //value is nullable here, but the precondition made sure that it isn't }
}
How can I reference a deep inner value to be non-null and then when accessed it is autotyped to be non-null
I absolutely hate using !! and especially error("...") if the chain of access calls are all on non-settable values (they are all val)
u
Uzi Landsmann
09/28/2020, 9:24 AM
I think if you write this before your `when`then the compiler will consider it safe to run without risk for NPE:
Copy code
if (someObject.someField.hisField.finalField == null) {
// do something that exits the function
}