Hey guys. I'm wondering what your pattern is with ...
# compose
f
Hey guys. I'm wondering what your pattern is with having`MutableState` holders with constructors. As a simple example:
Copy code
class SomeStateObject<T>(
    var someValue: T 
)
Now we want to make
someValue
react to state changes. So we can do something like this:
Copy code
class WithComposeReaction<T>(
    someValue:T
){
    var someValue = mutableStateOf(someValue)
}
But now we need to declare every field like this twice. Java-esque. So another way is doing this:
Copy code
class WithComposeReaction<T>(
    someValue: MutableState<T> 
)
But that fucks up the constructor API. Finally, we can forego some type safety and arrive at this
Copy code
class WithComposeReaction<T> {
    var someValue: T? by mutableStateOf(null)
}
And then we change
someValue
after construction. No solution is very good 😕 So I'm wondering, do you have any creative approaches to solve this?
g
I think 2 and 3 is fine, depending on case Not sure what else can be done, because or you pass reactive wrapper to constructor or you have to create it
m
personally, i prefer passing a constructor parameter with the initial value, and then having he mutable state be accessible by a property delegate. I'm not a big fan of having it artificially nullable just to defer the user setting it later.
Copy code
class WithComposeReaction<T>(
    initialValue:T
) {
    var someValue by mutableStateOf(initialValue)
}
3