effectful-core
Safe HaskellSafe-Inferred
LanguageGHC2021

Effectful.ReturnWith.Static

Description

Support for early return from a computation.

>>> import Control.Monad (when)
>>> :{
  classify :: ReturnWith String :> es => Int -> Eff es String
  classify n = do
    when (n < 0) $ returnWith "negative"
    when (n == 0) $ returnWith "zero"
    pure "positive"
:}
>>> runEff . runReturnWith $ classify 5
"positive"
>>> runEff . runReturnWith $ classify (-5)
"negative"

Interaction with threads

The ReturnWith effect uses runtime exceptions underneath, so the usual rules apply. In particular, in multi-threaded code a call to returnWith in a child thread will not automatically propagate to the parent. If you need that, use functions such as withAsync from the Effectful.Concurrent.Async module of the effectful package (which propagate exceptions from child threads to their parents) or arrange the propagation yourself.

For more information see the documentation of the Concurrent effect.

Since: 2.7.0.0

Synopsis

Effect

data ReturnWith (r :: Type) :: Effect Source #

Provide the ability to return early with a value of type r.

Instances

Instances details
type DispatchOf (ReturnWith r) Source # 
Instance details

Defined in Effectful.ReturnWith.Static

newtype StaticRep (ReturnWith r) Source # 
Instance details

Defined in Effectful.ReturnWith.Static

newtype StaticRep (ReturnWith r) = ReturnWith ReturnWithId

Handlers

runReturnWith :: forall r es. HasCallStack => Eff (ReturnWith r : es) r -> Eff es r Source #

Run a computation that can return early with a value of type r.

Operations

returnWith Source #

Arguments

:: forall r es a. (HasCallStack, ReturnWith r :> es) 
=> r

The value.

-> Eff es a 

Return early with the given value.