https://kotlinlang.org logo
#arrow
Title
# arrow
c

conner

11/27/2023, 9:58 PM
I don't know if
inikio
is exactly part of arrow (I can delete if this is an inappropriate venue), but my question should be generic to any tagless-style interpreter -- is there a way to compose interpreters when using the
suspend
approach? I might also be misunderstanding the "right" way to do this. Using the
inikio
example:
Copy code
tailrec fun <A> Casino<A>.execute(): A =
  when(this) {
    is Done -> result
    is FlipCoin -> next(Random.nextFlipOutcome()).execute()
  }
What if I e.g. wanted to log something whenever
FlipCoin
is interpreted?
a

Alejandro Serrano.Mena

11/28/2023, 8:16 AM
the easiest way is to make whatever other interpreter you need into the rewceiver
Copy code
tailrec fun <A> OtherInterpreter.execute(casino: Casino<A>): A = when (this) {
  is Done -> result
  is FlipCoin -> {
    val outcome = Random.nextFlipOutcome()
    operationFromOtherInterpreter(outcome)
    execute(next(outcome))
  }
}
c

conner

11/28/2023, 2:10 PM
Thank you! Is there an equivalent to something like
Coproduct
, where each interpreter doesn't have to be aware of the others?
a

Alejandro Serrano.Mena

11/28/2023, 2:31 PM
that depends on your use case with the advent of context receivers you would be able to write
context(Casino, OtherTagless) fun doSomething(): A
and then interpret as you wish
c

conner

11/28/2023, 3:40 PM
oh yeah, great point. thanks again! (and thanks for the library and walkthrough docs!)