Skuld.Comp.IIntercept behaviour (skuld v0.2.3)

View Source

Behaviour for effects supporting local interception via catch clauses.

This enables catch clauses in comp blocks to intercept effect-specific values. Currently used by:

  • Throw - intercepts errors for recovery
  • Yield - intercepts yields for response generation

Example

defmodule Throw do
  @behaviour Skuld.Comp.IIntercept

  @impl Skuld.Comp.IIntercept
  def intercept(comp, handler_fn) do
    catch_error(comp, handler_fn)
  end
end

Usage in comp blocks

comp do
  x <- risky_operation()
  x * 2
catch
  {Throw, :not_found} -> return(:default)  # calls Throw.intercept/2
end

The handler_fn receives the intercepted value (effect-specific) and must return a computation.

See Also

Summary

Callbacks

Intercept effect operations locally within a computation.

Callbacks

intercept(comp, handler_fn)

@callback intercept(
  comp :: Skuld.Comp.Types.computation(),
  handler_fn :: (term() -> Skuld.Comp.Types.computation())
) :: Skuld.Comp.Types.computation()

Intercept effect operations locally within a computation.

The handler_fn receives the intercepted value (effect-specific) and must return a computation:

  • For Throw: receives the error, returns recovery computation
  • For Yield: receives the yield value, returns computation producing resume input