natario1
11/03/2021, 1:29 PMclass Obj(val id: Int) {
@Composable fun id() = remember { id }
}
// later ...
var obj by remember { mutableStateOf(Obj(0)) }
LaunchedEffect(obj) {
delay(1000)
obj = Obj(obj.id + 1)
}
require(obj.id == obj.id())
I understand what's happening and I know how to solve it (use remember(this)
in Obj, or key
) but I wonder if this is how it should be. Wouldn't it be desirable that the compiler recognizes obj
as part of the remember call stack? Since id()
is a member function, it's pretty clear that if the object changes, the old remember should be invalidated.Zach Klippenstein (he/him) [MOD]
11/03/2021, 10:33 PMit’s pretty clear that if the object changes, the old remember should be invalidated.It might be for your use case, but that’s not necessarily true of all cases. It’s very possible that a class’s composable method might want to retain state across different receivers.
Zach Klippenstein (he/him) [MOD]
11/03/2021, 10:35 PMnatario1
11/03/2021, 11:14 PMnatario1
11/03/2021, 11:21 PMZach Klippenstein (he/him) [MOD]
11/03/2021, 11:44 PMimagine you’re just writing the Obj class without knowing how it will be usedWell that’s almost always going to be a disaster. Put another way, if someone really wants to misuse an API from how it was designed, they’re gonna have a bad time.
One could argue that composing with member functions breaks the stateless functional programming paradigmHow so? The method’s receiver is effectively just another argument to the function, with slightly different syntactic rules but from a data flow perspective it’s just another argument.
natario1
11/04/2021, 12:18 AMZach Klippenstein (he/him) [MOD]
11/04/2021, 3:32 AMid()
method is for. Why not just read the id
property directly? And I don't really understand the rest of that code either, it could just be this:
val id by remember { mutableStateOf(0) }
LaunchedEffect(Unit) {
delay(1000)
id++
}