elect
07/24/2018, 5:55 PMif (io.imeSetInputScreenPosFn != null && (g.platformImePos - g.osImePosSet).lengthSqr > 0.0001f)
io.imeSetInputScreenPosFn(g.platformImePos.x.i, g.platformImePos.y.i) // error, Reference has a nullable type
where val imeSetInputScreenPosFn: ((x: Int, y: Int) -> Unit)?
But I should profit from the smart cast from the if
that ensures it's not nullrook
07/24/2018, 6:04 PMimeSetInputScreenPosFn
is a var
, so the compiler (correctly) thinks that it can become null if there’s an asynchronous operation that changes it.elect
07/24/2018, 6:04 PMval
, that's the problem..rook
07/24/2018, 6:06 PMelect
07/24/2018, 6:06 PMrook
07/24/2018, 6:08 PMio
nullable?elect
07/24/2018, 6:08 PMelect
07/24/2018, 6:08 PMelect
07/24/2018, 6:08 PMkarelpeeters
07/24/2018, 6:52 PMio.imeSetInputScreenPosFn
isn't null, where both io
and imeSetInputScreenPosFn
are `val`s?rook
07/24/2018, 7:52 PMkarelpeeters
07/24/2018, 8:26 PMclass Test(val str: String?)
val test = Test("hey")
test.str.toUpperCase() //not allowed
if (test.str != null)
test.str.toUpperCase() //allowed
elect
07/24/2018, 8:30 PMio
is a var
, but is not nullablekarelpeeters
07/24/2018, 8:34 PMio
reference could have changed between the check and the indexing.rook
07/24/2018, 9:29 PMclass SomeClass(val str: String?)
var test = SomeClass("yo")
fun main() {
test.str.toUpperCase() //Only safe calls etc. (use ?.)
if(test.str != null) {
test.str.toUpperCase() //Smart cast to String is impossible, because 'test.str' is a complex expression
}
}
karelpeeters
07/24/2018, 9:30 PMvar
. Make it a val
and it'll work.rook
07/24/2018, 9:31 PMkarelpeeters
07/24/2018, 9:32 PMelect
07/25/2018, 7:26 AMrook
07/25/2018, 3:35 PM