Skip to content

Instantly share code, notes, and snippets.

@Krever
Last active April 2, 2024 05:23
Show Gist options
  • Select an option

  • Save Krever/e82a99ef24b37ef4d9e4fe574d6ed04b to your computer and use it in GitHub Desktop.

Select an option

Save Krever/e82a99ef24b37ef4d9e4fe574d6ed04b to your computer and use it in GitHub Desktop.

Revisions

  1. Krever revised this gist Apr 2, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wio.scala
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ trait WorkflowContext {
    }


    object WithdrawalContext {
    object WithdrawalContext extends WorkflowContext {
    type Event = WithdrawalEvent
    type State = WithdrawalState
    }
  2. Krever created this gist Mar 31, 2024.
    41 changes: 41 additions & 0 deletions wio.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // 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 {
    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()
    )


    }