Use of evaluate in Control.Concurrent.MVar

Roman Cheplyaka roma at ro-che.info
Mon Oct 12 18:20:12 UTC 2020


Hi David,

On 12/10/2020 21.01, David Feuer wrote:
> Couldn't we just do this?
> 
> modifyMVar m io =
>   mask $ \restore -> do
>     a      <- takeMVar m
>     (a',b) <- restore (io a >>= (pure $!)) `onException` putMVar m a
>     putMVar m a'
>     return b

No — the haddocks for 'evaluate' in Control.Exception explain the difference between the two. In particular,

> The rule of thumb is to use evaluate to force or handle exceptions in lazy values. If, on the other hand, you are forcing a lazy value for efficiency reasons only and do not care about exceptions, you may use return $! x.

If you want to know why, I think you can find some old bugs in the ghc bug tracker that resulted from an incorrect usage of return $! ... where evaluate was warranted.

Roman


More information about the Libraries mailing list