ritesh
03/23/2022, 6:56 AMsealed
class ?
sealed class MyClass {
object A : MyClass()
data class SomeClass(val value:String): MyClass()
}
gildor
03/23/2022, 7:10 AMgildor
03/23/2022, 7:10 AMritesh
03/23/2022, 7:12 AMritesh
03/23/2022, 7:13 AMritesh
03/23/2022, 7:16 AMgildor
03/23/2022, 7:20 AMgildor
03/23/2022, 7:21 AMgildor
03/23/2022, 7:21 AMgildor
03/23/2022, 7:22 AMAlso, it should be @Immutable i believeYep, you right, Immutable
ritesh
03/23/2022, 7:25 AMBut you can try, it’s easy to checkAhh sure. thanks!
myanmarking
03/23/2022, 9:19 AMritesh
03/23/2022, 4:18 PMritesh
03/23/2022, 4:21 PMval
but use var
data class SomeClass(var value:String)
But re-comp was skipped in this case well, i believe it doesn't matter if it's var
or val
, compose considers all primitives as stable as mentioned in the documentation.myanmarking
03/23/2022, 4:23 PMmyanmarking
03/23/2022, 4:25 PM@Composable
fun SomeClassComposable(item: SomeClass){
Text(item.value)
}
SomeClassComposable entered body, but probably text will skip because content equalsritesh
03/23/2022, 4:38 PMvar
or val
does makes a difference.
@Composable
fun ComposeTest(vm:MyVm){
val dataClassstate = vm.state.value
val sealedState = vm.sealedState.value
LogCompositions(tag = "ComposeTest")
Column {
ButtonComposable(onClick = vm::valueChanged) // it changes sealedState, not dataClassstate state
SomeClassRecomp(someClass = dataClassstate)
}
}
@Composable
fun SomeClassRecomp(someClass: SomeClass){
LogCompositions(tag = "SomeClassRecomp")
Text(text = someClass.value)
}
On Button click i am changing sealedState
which ends of re-composing ComposeTest
on every button click.
If my SomeClass
holds a var
, SomeClassRecomp
gets called irrespective of any change in it's state. But if i change it to val
, it won't get called.ritesh
03/23/2022, 4:52 PMSomeClassComposable entered body, but probably text will skip because content equalsyeah and also it won't enter the SomeClassComposable body, if SomeClass is stable and haven't changed, it will enter even if equals returns true and but it isn't stable (example using
var
instead of val
)ritesh
03/23/2022, 5:00 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 5:39 PMtasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
freeCompilerArgs += [
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=${buildDir}/composeReports"
]
}
}
Will spit out a file named something like *-classes.txt
that describes what’s inferred for each class.
So for your case, I get:
stable class A {
<runtime stability> = Stable
}
stable class SomeClass {
stable val value: String
<runtime stability> = Stable
}
stable class MyClass {
<runtime stability> = Stable
}
ritesh
03/23/2022, 5:42 PMZach Klippenstein (he/him) [MOD]
03/23/2022, 5:43 PMritesh
03/23/2022, 9:51 PMsealed class SealedClass{
object Loading : SealedClass()
data class SomeClass(val error:String):SealedClass()
}
stable class Loading {
<runtime stability> = Stable
}
stable class SomeClass {
stable val error: String
<runtime stability> = Stable
}
stable class SealedClass {
<runtime stability> = Stable
}
ritesh
03/23/2022, 9:53 PM<runtime stability>
means? It's stability will be resolved at run-time? but it says Stable though
<runtime stability> = Stable
ritesh
03/23/2022, 10:04 PMvar
is stable, but at run-time it makes the class unstable. What does that mean, if just use value
in a composable function, re-comp will be skipped, even though if it's a var
?
class AA {
var value:String = "String"
}
unstable class AA {
stable var value: String
<runtime stability> = Unstable
}
class AA {
val value:String = "String"
}
stable class AA {
stable val value: String
<runtime stability> = Stable
}
Zach Klippenstein (he/him) [MOD]
03/23/2022, 10:40 PMvar
is stable?Zach Klippenstein (he/him) [MOD]
03/23/2022, 10:41 PMritesh
03/24/2022, 4:17 AMunstable class AA {
stable var value: String
<runtime stability> = Unstable
}
Yeah, I was referring to the fact that compiler says it's stable stable var value: String
but also marks it as unstable
at run-time. I think i got confused between run-time and compile-time stability inference.
Makes sense in case of var
ThanksZach Klippenstein (he/him) [MOD]
03/24/2022, 4:20 AM