Therefore, what is the best way to force the type of the value to be not nullable? The method get() thinks it is possible that the item is missing and returns type Int?. Something.toLong() // Error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?" But I have an error: val map = mapOf("a" to 65,"b" to 66,"c" to 67) How do I change or treat a value as not null without requiring the if check, assuming I know for sure it is truly never null? For example, here I am retrieving a value from a map that I can guarantee exists and the result of get() is not null. Something.foo() // Error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Xyz?"īut if I check null first it is allowed, why? val something: Xyz? = createPossiblyNullXyz() What is the idiomatic way of doing so in Kotlin?įor example, this code is in error: val something: Xyz? = createPossiblyNullXyz() If I have a nullable type Xyz?, I want to reference it or convert it to a non-nullable type Xyz.
0 Comments
Leave a Reply. |