【发布时间】:2016-01-18 11:00:30
【问题描述】:
我正在使用 turtle 在 Haskell 中编写一个 shell 脚本,并且想知道编写可能失败的命令的最佳实践。
现在我有一个案例表达式楼梯,如下所示:
runRemote :: MonadIO io => Text -> Text -> io ()
runRemote oldVersion' newVersion' = sh $ do
mkdir "out"
e1 <- shell ("command " <> oldVersion') empty
case e1 of
ExitFailure n -> cleanup
ExitSuccess -> do
e2 <- shell ("command " <> newVersion') empty
case e2 of
ExitFailure n -> cleanup
ExitSuccess -> do
curDir <- pwd
cd (curDir <.> oldVersion')
e3 <- shell ("command something else") empty
case e3 of
-- ...
-- And so on...
如果case 表达式在Maybe 类型上扩展,解决方案是派生Monad 实例。
库作者没有为ExitCode 派生Monad 实例是否有特殊原因,或者是否有更好的方法来处理Haskell shell 代码的错误?
【问题讨论】:
-
不可能为
ExitCode创建一个Monad实例,因为ExitCode有一种*并且Monad类型类需要一个类型为* -> *的类型(一种有一个类型参数)。