Haskell 命令行脚本应该如何报告来自 `read` 的错误?
How should a Haskell command line script report errors from `read`?
我不确定报告错误的正确方法 read
在 main = do ...
.
中创建实例以响应用户在命令行中的输入
具体来说,我有一个 constructor 可以验证其参数并以通常的方式报告错误
-- ...
Right cfg -> cfg
Left err -> error (show err)
以及使用此构造函数的 read
的实现。然而,在我的实现中,我有一个 cryptic note (继承自一些我已经忘记的很久以前的研究),它删除了错误信息
instance Read ... where
readsPrec _ i = case ...
Right cfg -> [(cfg, "")]
Left _ -> [] -- Loses error information, but conforms to specification of 'readsPrec' in 'Read'
main :: IO ()
main = do
... read
仅将构造函数生成的所有错误报告为
Prelude.read: no parse
如果我忽略我的神秘评论和 instead have
Left err -> error (show err)
在构造函数中,然后我得到完整的报错信息给用户:
script: Detailed error information here
CallStack (from HasCallStack):
error, called at ./Pkg/Module.hs:371:57 in main:Pkg.Module
(虽然有堆栈和行信息,但我不想在这种情况下报告)。
所以我有三个相关的问题:
- 实际上有什么理由不使用
Left err -> error (show err)
报告来自 read
的完整错误信息吗?
- 如果需要
Left err -> []
之类的东西,我该如何在命令行报告 read
或构造函数错误?
和(没那么重要)
- 如何在我的
main = do ...
中从 read
报告时删除行和堆栈信息?
- 是:纯代码无法捕获
error
调用。从纯代码中知道 read
(嗯,reads
)失败通常很有用。
- 使用不同于
Read
的 class 进行解析,让您更清楚地报告错误。每个流行的解析器组合器库都具有良好的错误报告功能。
- 使用富有想象力的命名
errorWithoutStackTrace
。
我不确定报告错误的正确方法 read
在 main = do ...
.
具体来说,我有一个 constructor 可以验证其参数并以通常的方式报告错误
-- ...
Right cfg -> cfg
Left err -> error (show err)
以及使用此构造函数的 read
的实现。然而,在我的实现中,我有一个 cryptic note (继承自一些我已经忘记的很久以前的研究),它删除了错误信息
instance Read ... where
readsPrec _ i = case ...
Right cfg -> [(cfg, "")]
Left _ -> [] -- Loses error information, but conforms to specification of 'readsPrec' in 'Read'
main :: IO ()
main = do
... read
仅将构造函数生成的所有错误报告为
Prelude.read: no parse
如果我忽略我的神秘评论和 instead have
Left err -> error (show err)
在构造函数中,然后我得到完整的报错信息给用户:
script: Detailed error information here
CallStack (from HasCallStack):
error, called at ./Pkg/Module.hs:371:57 in main:Pkg.Module
(虽然有堆栈和行信息,但我不想在这种情况下报告)。
所以我有三个相关的问题:
- 实际上有什么理由不使用
Left err -> error (show err)
报告来自read
的完整错误信息吗? - 如果需要
Left err -> []
之类的东西,我该如何在命令行报告read
或构造函数错误?
和(没那么重要)
- 如何在我的
main = do ...
中从read
报告时删除行和堆栈信息?
- 是:纯代码无法捕获
error
调用。从纯代码中知道read
(嗯,reads
)失败通常很有用。 - 使用不同于
Read
的 class 进行解析,让您更清楚地报告错误。每个流行的解析器组合器库都具有良好的错误报告功能。 - 使用富有想象力的命名
errorWithoutStackTrace
。