Zyle Moore
08/29/2025, 4:44 AMval
properties. It's defined in a core
module. I'm using it in a web
module. Sometimes it can be smart-cast to its non-nullable version, but other times it can't. I thought that if I wrap usage in an if (nullableProp != null) { doSomethingWith(nullableProp.toString()) }
, it would be safe, but it's not. Browsing for answers points to cases where it's a var
, which could be reassigned between dereferencing calls. Or, the other module can be recompiled(?) and not be nullable anymore. But surely, a simple data class with `val`s is safe, right?
I was able to swap a `forEach`'s it
with the deconstructed props, and it works fine then, so it seems like it's related to dereferencing it more than once, since deconstructing (having a single variable for the property) can smart cast, even though it's still a Public API in a different moduleZyle Moore
08/29/2025, 4:52 AMdata class Something(val somethingStatic: String) {
val flipFlop: String?
get() =
if (randomInt().even) { somethingStatic }
else { null }
}
Is that why?CLOVIS
08/29/2025, 7:27 AMCLOVIS
08/29/2025, 7:28 AMif (foo.nulableProp != null) {
doSomethingWith(foo.nullableProp)
}
by
val nullableProp = foo.nullableProp
if (nullableProp != null) {
doSomethingWith(nullableProp)
}
This way, the smart-cast worksCLOVIS
08/29/2025, 7:28 AMval
to a var
, it's also not safe to smart-cast anymore