I just tried explicit backing fields today... and ...
# getting-started
s
I just tried explicit backing fields today... and they're a little disappointing. Tried the most famous use-case:
Copy code
class ViewModel {
    val count: StateFlow<Int>
        field = MutableStateFlow(0)
}
And this looks very nice and all... but has a big drawback, which is that relying on just the type conversion to a superclass is not enough to enforce what we really want here. I can very easily do this:
Copy code
(viewModel.count as MutableStateFlow<Int>).value = 1
And this code would work just fine, which is not great. Same thing applies to stuff like
MutableList
->
List
. The usual approach is to use
asStateFlow()
here, which would actually prevent modifications through type casting, because it converts the underlying type to
ReadonlyStateFlow
, which can't be modified. I don't know if this is already planned, but it would be great if we can provide custom initializers like this:
Copy code
class ViewModel {
    val count: StateFlow<Int> = field.asStateFlow()
        field = MutableStateFlow(0)
}
where we can use the
field
in the initializer.
Wait a sec... I just tried this, and it seems to work just fine:
Copy code
class ViewModel {
    val myState: StateFlow<Int>
        field = MutableStateFlow(0)
        get() = field.asStateFlow()
}
Trying to cast results in
ClassCastException
as intended. The syntax for this isn't really bad, but this is already 3 lines long, so IDK, maybe allowing this in the initializer would still be a good idea?
d
I'm not sure what you expect. If you assign it as is, its going to be castable to the type you assign.
j
On a side note, what exactly are you trying to prevent by using
asStateFlow()
? On the JVM, people can do all sorts of things by reflection, so you can't really protect your code from people who actually want to break it. If people use
as
on purpose, they're looking for trouble, I don't see a huge point in trying to prevent it. The most important IMO is to prevent accidental misuse, so the most important to me is to expose a read-only type so people don't actually mutate it.
s
Ah, that actually makes sense. So it's not as big of a deal as I made it out to be 😅
👍 1
107 Views