So, I have the following: ```if (io.imeSetInputScr...
# announcements
e
So, I have the following:
Copy code
if (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 null
r
I’m assuming
imeSetInputScreenPosFn
is a
var
, so the compiler (correctly) thinks that it can become null if there’s an asynchronous operation that changes it.
e
no, it's a
val
, that's the problem..
r
Ah, I just read the first block and didn’t read the rest, sorry about that..
e
np
r
Wait, is
io
nullable?
e
neither
wait
no, it's not, confirmed
k
The compiler doesn't realize that
io.imeSetInputScreenPosFn
isn't null, where both
io
and
imeSetInputScreenPosFn
are `val`s?
r
That’s what he’s saying
k
That doesn't sound right to me:
Copy code
class Test(val str: String?)
val test = Test("hey")

test.str.toUpperCase()      //not allowed
if (test.str != null)
    test.str.toUpperCase()  //allowed
e
io
is a
var
, but is not nullable
k
Then of course that isn't allowed, the
io
reference could have changed between the check and the indexing.
r
Copy code
class 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
    }
}
k
Because test is a
var
. Make it a
val
and it'll work.
r
Yeah, was just clarifying the error for posterity. The original message made it seem like the smart cast had unexpected behavior, but it’s not unexpected.
k
Ah okay, thanks!
e
yeah, that makes sense, thanks guys and sorry for the trouble
r
No trouble at all, friend
👍 1