Understanding the bracket function

Looking at the bracket Parallel and Parallel Programming feature in Haskell :

bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket before after during = do
  a <- before
  c <- during a `onException` after a
  after a
  return c

In the case of an exception, why is the function aftercalled only once? In other words, I am confused at explicit execution after atwice in case of exception.

But, the code shows that after aonly, as I understand it, is only called once:

λ: >data MyException = MyException deriving Show
λ: >instance Exception MyException

λ: >let e = return MyException :: IO MyException

λ: >bracket e (const $ putStrLn "clean up!") return
clean up!
MyException
+4
source share
1 answer

From the docs tobracket :

, , , bracket, bracket , . , bracket ( ).

onException:

finally, , , .

, , during a, after a, , after a; , .

, , - , throw throwIO :: Exception e => e -> IO a.

+6

Source: https://habr.com/ru/post/1688082/


All Articles