为什么括号需要`release`来产生被忽略的结果?

sev*_*evo 2 haskell exception-handling

bracket
        :: IO a         -- ^ computation to run first (\"acquire resource\")
        -> (a -> IO b)  -- ^ computation to run last (\"release resource\")
        -> (a -> IO c)  -- ^ computation to run in-between
        -> IO c         -- returns the value from the in-between computation
bracket before after thing =
  mask $ \restore -> do
    a <- before
    r <- restore (thing a) `onException` after a
    _ <- after a
    return r
Run Code Online (Sandbox Code Playgroud)

这与某些API设计模式或约定有关吗?为什么不使用以下部分签名?

        -> (a -> IO ())  -- ^ computation to run last (empty result)
Run Code Online (Sandbox Code Playgroud)

要么

        -> (a -> IO a)  -- ^ computation to run last (`a` cannot be ignored)
Run Code Online (Sandbox Code Playgroud)

Eri*_*ikR 7

我认为你有倒退 - bracket会忽略你的释放操作的结果.

如果释放操作的签名是a -> IO ()那么你将不得不提供一个总是返回的函数().通过签名,a -> IO b您的发布函数可以返回任何内容,因为类型变量b未在签名中的任何其他位置引用 - 它与任何其他类型变量完全无关.

  • `main`可以将`IO a`或`IO Int`作为一种类型. (3认同)