https://kotlinlang.org logo
#compose
Title
# compose
k

Kefas

10/05/2022, 9:54 AM
How is this data class inferred as unstable? Which contract it doesn't comply with?
Copy code
data class User(val id: Int) {
    private var name = "a"
}

unstable class User {
  stable val id: Int
  stable var name: String
  <runtime stability> = Unstable
}
l

Lukáš Kúšik

10/05/2022, 10:12 AM
can't have vars in the class
k

Kefas

10/05/2022, 10:34 AM
It's a private var though, based on the contract, only the public properties are considered. And in this case the equals will forever be the same because the constructor property is a val
• The result of
equals
for two instances will forever be the same for the same two instances.
• If a public property of the type changes, Composition will be notified.
• All public property types are also stable.
z

Zach Klippenstein (he/him) [MOD]

10/05/2022, 3:03 PM
You can file a bug, but I think the inference is kind of meant to be “best effort”, to cover most common cases that are easy to prove correct. I’m guessing it doesn’t catch this because it would have to do a lot more complicated analysis to prove that your private var is never read by any public properties or methods.
3 Views