Last active
April 2, 2024 05:23
-
-
Save Krever/e82a99ef24b37ef4d9e4fe574d6ed04b to your computer and use it in GitHub Desktop.
context-dependent-wio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes! Thanks for spotting, let me fix that