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)
我认为你有倒退 - bracket会忽略你的释放操作的结果.
如果释放操作的签名是a -> IO ()那么你将不得不提供一个总是返回的函数().通过签名,a -> IO b您的发布函数可以返回任何内容,因为类型变量b未在签名中的任何其他位置引用 - 它与任何其他类型变量完全无关.
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |