https://kotlinlang.org logo
Title
d

dave08

05/23/2023, 10:13 AM
I'm really struggling on this one... how do I get this to work?
@optics data class Bar(val stateFlow: MutableStateFlow<Foo>)
val foo = Foo(...)
val foo2 = Foo(...)
val bar = Bar(MutableStateFlow(foo))

inline val <T> T.current: arrow.optics.Iso<MutableStateFlow<T>, T> inline get() = arrow.optics.Iso(
    get = { stateFlow: MutableStateFlow<T> -> stateFlow.value },
    reverseGet = { value: T -> MutableStateFlow(value) }
)

val baz = bar.copy {
  Bar.stateFlow.current set foo2 // the IDE says that `set` needs an infix modifier in PIso
}
otherwise, am I doing this right?
a

Alejandro Serrano Mena

05/23/2023, 11:10 AM
interesting… I think the IDE is getting confused about where
set
is coming from in this case
if you do
Bar.stateFlow.current.set(foo2)
instead, does it help?
d

dave08

05/23/2023, 11:11 AM
Truth is, in my real case T : Option<V> ... does that make any difference?
if you do
Bar.stateFlow.current.set(foo2)
instead, does it help?
nope.. I think my problem has to do with trying to combine the generated optics with this one and my receiver is T...
When I try to compose them, it doesn't work... and I'm struggling to get the types right
Here the compose function is red:
inline val <S : Any, T> arrow.optics.Lens<S, MutableStateFlow<T>>.currentOpt: arrow.optics.Iso<S, Option<T>> inline get() = this compose arrow.optics.Iso(
    get = { stateFlow: MutableStateFlow<Option<T>> -> stateFlow.value },
    reverseGet = { value: Option<T> -> MutableStateFlow(value) }
)
image.png
I FINALLY got something that works...
inline val <S, T> Setter<S, MutableStateFlow<Option<T>>>.value: Setter<S, Option<T>>
    inline get() = this compose arrow.optics.Iso(
        get = { stateFlow: MutableStateFlow<Option<T>> -> stateFlow.value },
        reverseGet = { value: Option<T> -> MutableStateFlow(value) }
    )
s

stojan

05/25/2023, 8:14 AM
hm... a bit strange to have a
MutableStateFlow
as a property in a data class.... typically you have the
MutableStateFlow
on the outside, and data is inside
d

dave08

05/25/2023, 8:57 AM
This is a statemachine that needs to listen to states from the outside so I can't just pass in the first state.. on the other hand optics helps me configure my fixtures (including that state) so I wanted to just do a copy and set the whole fixture with optics