Say there's a class in a library: ```interface Foo...
# getting-started
d
Say there's a class in a library:
Copy code
interface FooBase<S> {
  val state: Flow<S>
}

public abstract class Foo<S>(): FooBase<S> {
  override val state: Flow<S> get() { ... }
}
// I'd like to have this as a state flow (I need to pass this to users of MyFoo, because there could be other implementations... but they need StateFlow, so I can't use Foo):
abstract class MyFooBase : Foo<Bar> {
   override abstract val state: StateFlow<Bar>
}

class MyFoo : MyFooBase<Bar> {
  override val state: StateFlow<Bar> by lazy {
    super.state...stateIn(...)
  }
}
But I get an error
abstract-member-cannot-be-access-directly-kotlin
... is there any way to do this?
s
I would approach this using association rather than inheritance. Instead of inheriting from
Foo
, can you just make a new class that implements
FooBase
and delegates to a private instance of
Foo
?
d
Foo is an abstract class.... so I'd have to implement it and delegate to it... Foo is really https://github.com/freeletics/FlowRedux/blob/main/flowredux/src/commonMain/kotlin/com/freeletics/flowredux/dsl/FlowReduxStateMachine.kt
That's a good idea, just wondering how to do it with the least boilerplate...
s
Does Kotlin’s built-in delegation help? Building on the example code you shared it would look something like this:
Copy code
class MyFoo<S>(delegate: FooBase<S>, scope: CoroutineScope): FooBase<S> by delegate {
    override val state: StateFlow<Bar> by lazy {
        delegate.state.stateIn(scope)
    }
}
d
delegate { } imported: import androidx.compose.ui.text.platform.EmojiCompatStatus.delegate.... 😂
s
😄
Maybe my syntax isn’t quite right
d
I'd have to use by object : FlowReduxStateMachine { ... }
Actually, I could just make a wrapper class around the SM and just delegate the functionality I want to expose to the wrapped class w/o Kotlin's delegation (which copies over all the functionality per se...). Thanks for the idea!