[Haskell-cafe] STM rollback?

YueCompl compl.yue at icloud.com
Mon Nov 30 06:21:29 UTC 2020


The exception based solution is the best as far as I can tell for now (But I'm not authoritative of course). Maybe a dedicated `Rollback` algebraic effect and its respective handler would feel less "icky"? But the machinery is not generally available yet, and maybe an algebraic effects based solution will be overkill taking the ergonomic cost it'll impose, even if practically doable?

Regards,
Compl


> On 2020-11-29, at 05:05, amindfv--- via Haskell-Cafe <haskell-cafe at haskell.org> wrote:
> 
> I'd like to be able to give up on an STM transaction: roll back and don't retry.
> I've cooked up something with exceptions but it feels a bit icky to use exceptions for something like this - is there a better way?:
> 
>    data Rollback = Rollback deriving (Show)
>    instance Exception Rollback
> 
>    rollback :: STM x
>    rollback = throwSTM Rollback
> 
>    atomicallyWithRollback :: STM x -> IO (Maybe x)
>    atomicallyWithRollback a =
>       (Just <$> atomically a)
>          `catch` (\Rollback -> pure Nothing)
> 
> The alternative I've found is something like:
> 
>    otherWay :: STM x -> IO (Maybe x)
>    otherWay a =
>       atomically $ (Just <$> a) `orElse` pure Nothing
> 
> But this turns any "retry" in "a" into a rollback, and I'd like to have the option to do either (retry or rollback).
> 
> Thanks,
> Tom
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.



More information about the Haskell-Cafe mailing list