Open BKMbigo opened 1 year ago
// Does not crash
import kotlinx.coroutines.Job
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow {
var job: Job? = null
Text("Good Morning")
}
}
Turns out that the problem has nothing to do with Kotlin coroutines but is related to any call remember { null }
// Crashes
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow {
var bar: Bar? = remember { null }
Text("Good Morning")
}
}
class Bar
Creating a generic function that returns null
// Does not crash
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow {
var bar: Bar? = remember { functionThatReturnsNull() }
Text("Good Morning")
}
}
class Bar
fun <T> functionThatReturnsNull(): T? = null
This can be used as a workaround if remember { null }
is needed
Trying kotlin Unit
// Does not crash
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow {
var bar: Unit = remember { null }
Text("Good Morning")
}
}
Trying kotlin stdlib classes
// Crashes
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow {
var bar: Int? = remember { null }
Text("Good Morning")
}
}
Explicitly stating type in remember does not help
// Crashes
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow {
var bar: Int? = remember<Int?> { null }
Text("Good Morning")
}
}
Crash still present on Kotlin 1.9.0 and compose 1.4.0-dev-wasm09
Compose/WASM crashes whenever such references are encountered
Example:
The canvas will not display the Text "Good Morning" and no exception is thrown
However, references in Side Effects do not throw the exception... Example: