[Haskell-cafe] STM rollback?

amindfv at mailbox.org amindfv at mailbox.org
Sat Nov 28 21:05:52 UTC 2020


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




More information about the Haskell-Cafe mailing list