Hi, I'm playing with `explicit backing fields` ``...
# getting-started
u
Hi, I'm playing with
explicit backing fields
Copy code
interface VersionChecker {
    val supportedVersion: Flow<Boolean>
}

class VersionCheckerImpl(
    ...
) : VersionChecker {
    override val supportedVersion: Flow<Boolean>
        field = MutableStateFlow(true)

    ...
}
and this fails to compile with
Properties with explicit backing fields must be final.
Which granted is true, but - is really this the desired outcome? In a production app, it's most likely that this will be the pattern (exposed via interface), rendering the feature kinda useless
š
The production version is more like:
Copy code
private val mutableSupportedVersion = MutableStateFlow(true)
override val supportedVersion: Flow<Boolean>
  get() = mutableSupportedVersion
Or you can assign directly and hope no one re-types it at runtime:
Copy code
override val supportedVersion: Flow<Boolean> = MutableStateFlow(true)
field
is useful for local transformation during assignment or validation, but there are other ways, too. This is probably the easiest though.
u
Hm? Explicit backing fields is a new syntactic sugar for syntax you spelled out
š
Sorry, my bad. I misunderstood your original post.
Anyway, you can mark your property in VersionCheckerImpl as
final override
, can’t you? In many cases, you don’t need to extend the implementation class further, just to implement the interface. The feature would still be useful in these scenarios.
u
I mean the class is final so that cant happen but ill try