Is there any way to check if a Kotlin type is immu...
# announcements
a
Is there any way to check if a Kotlin type is immutable at compile time (i.e. with some API in
org.jetbrains.kotlin.types
or
<http://org.jetbrains.kotlin.ir|org.jetbrains.kotlin.ir>
?
j
No. Mutable objects can, by all outward appearance, be immutable.
a
Sorry I should have clarified initially, I'm curious at compile time via compiler APIs.
j
You could guess, but it will be the halting problem very quickly.
Copy code
class Foo {
  val value get() = System.nanoTime()
}
vs
Copy code
class Bar {
  val value get() = System.lineSeparator()
}
Bar is by all accounts immutable, Foo is not. How do you differentiate?
a
Yea, makes sense. I'm working on inferring stable composables in https://android-review.googlesource.com/c/platform/frameworks/support/+/1221903 , and it seems like the best way might jut be hard-coding some types like String.
j
Yeah I suspect you can get pretty far with simple heuristics