by ReleaseCandidat · 2y ago · 0 comments
With `with` you explicitely set the effect handler, no magic involved.
> Also, the `fun yield( x : a ) : ()` syntax is highly unintuitive. It _takes_ an int and returns empty? So it's just a continuation?
A function that does not return anything is (the poster child of) an impure function, that does nothing else but an (side) effect. That's why you need to call it as an effect and not a "normal" (pure) function.
> And magically it transfers control flow to the correct place? How does it know where that is?
It doesn't have to. The hidden `run` function (or whatever you want to call it) of the effect systém that actually calls the right effect handler has to know about that.
The run function does something like (simplified)
- see, that a effect handler with ID 'yield' is called
- call the (currently registered) handler for 'yield' with the given arguments
- call the continuation with the result of the handlerAfter that depends on whether a continuation actually needs to be captured or not. Many useful effects can be implemented as `val` or `fun` clauses which can be called / accessed in place without any unwinding of the stack. Only in cases where a continuation is required is the continuation built up.