Skip to content

Instantly share code, notes, and snippets.

@Krever
Last active April 2, 2024 05:23
Show Gist options
  • Save Krever/e82a99ef24b37ef4d9e4fe574d6ed04b to your computer and use it in GitHub Desktop.
Save Krever/e82a99ef24b37ef4d9e4fe574d6ed04b to your computer and use it in GitHub Desktop.
context-dependent-wio
// This is a very minimal example showcasing how we can levarage type members to capture fixed types without the need
// to introduce more type parameters
trait WorkflowContext {
type State
type Event
// operation that transforms In into Out, while guaranteeing the output is a substate os State
sealed trait WIO[Err, In, Out <: State]
object WIO {
// example variant where IO is guarded with an event, IO is not run during recovery.
// event is guaranteed to be subtype of Event
case class HandleIO[Err, In, Out <: State, Evt <: Event](runIO: In => IO[Evt], handleEvent: Evt => Either[Err, Out])
extends WIO[Err, In, Out]
}
}
object WithdrawalContext extends WorkflowContext {
type Event = WithdrawalEvent
type State = WithdrawalState
}
class WihdrawalWorkflow(service: WithdrawalService) {
// output type is specific to WithdrawalContext
// runing this workflow requires persistance for WithdrawalEvent
// at any point of time, we can get a state of the workflow which will be of type WitdrawalState
def workflow: WidrawalContext.WIO[Nothing, Unit, WithdrawalState.Complete] =
WIO.RunIO(
_ => service.doSth().as(WithdrawalEvent.SthDone),
sthDone => WiithdrawalState.Completed()
)
}
@Krever
Copy link
Author

Krever commented Apr 2, 2024

Shouldnt WithdrawalContext extend WorkflowContext?

Yes! Thanks for spotting, let me fix that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment