As far as I understand you can call throwSTM and get desired effect.


Alexander Vershilov

29 нояб. 2020 г., в 00:07, amindfv--- via Haskell-Cafe <haskell-cafe@haskell.org> написал(а):

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.