从模板 Haskell 拼接发出警告
我知道我可以通过从拼接调用 fail
来导致编译时错误,但是是否有可能只生成警告?我特别希望在使用 -Werror
编译时能够将此警告转换为错误。
本质上我想做的是:
todo :: Q Exp
todo = do
-- emit warning somehow
loc <- location
let message = ... -- generate message based on loc
[| error $(litE (stringL message)) |]
这个想法是在编码时使用 this 而不是 undefined
,但确保它不会通过使用 编译来潜入生产代码 -错误
。
myFunc x | isSimpleCase x = 42
| otherwise = $todo
I know that I can cause a compile-time error by calling fail
from a splice, but is it possible to only generate a warning? In particular I would like it to be possible to turn this warning into an error when compiling with -Werror
.
Essentially what I'm trying to do is this:
todo :: Q Exp
todo = do
-- emit warning somehow
loc <- location
let message = ... -- generate message based on loc
[| error $(litE (stringL message)) |]
The idea is to use this instead of undefined
while coding, but make sure it doesn't sneak its way into production code by compiling with -Werror
.
myFunc x | isSimpleCase x = 42
| otherwise = $todo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原来我想要的函数是 Template Haskell 函数
report
。它的类型签名在文档中,但我必须阅读源代码才能弄清楚它的作用。 TH 文档确实需要一些改进。不管怎样,我的
todo
占位符现在工作得很好,如果有人感兴趣的话,我很快就会在 Hackage 上发布一些东西。Turns out the function I was after was the Template Haskell function
report
. Its type signature was in the documentation, but I had to read the source code to figure out what it did. The TH documentation sure could use some improvements.Anyway, my
todo
placeholder is now working perfectly, and I'll put something up on Hackage soon if anyone is interested.我不认为这在 TH 中是可能的,但这是一个非常酷的想法。
实现它的一种方法是通过绑定到 GHC-API 警告和 调试输出 或
例如,假装是 GHC,
会产生
构造 GHC 警告,其工作方式应该类似,检查
-Werror
是否已设置,并且您可以清理 API,使其非常有用。I don't believe this is possible naively from TH, but it is a really cool idea.
One way to implement it would be via bindings to the GHC-API warning and debug output or error functions.
E.g. to pretend to be GHC,
produces
Constructing GHC warnings should work similarly, checking if
-Werror
is set, and you could clean up the API to be quite useful.要从 Template Haskell 拼接发出警告,
您可以使用
reportWarning :: String -> Q()
。它已经包含位置(行和列)。
您可以简单地通过以下方式实现您的
todo
函数:更多信息
@hammar 的答案指示了函数
report
。它自 GHC 7.6 (2012) 起已被弃用,并且可能会在不久的将来从 API 中删除。(但是,
报告
仍然可用关于 GHC 7.10< /a>
和
在 GHC 主分支上
截至 2015 年。)
使用
reportError
报告错误并继续进行Q
计算(无论如何最终编译失败)。
使用
fail
停止并出现错误 (GHC ≤ 7.10)。那可能不适用于 GHC 8.0。
To emit warnings from Template Haskell splices,
you can use
reportWarning :: String -> Q ()
.It already includes the location (line and column).
You can implement your
todo
function simply by:Further information
@hammar's answer indicates the function
report
. It has been deprecated since GHC 7.6 (2012) and will possibly be removed from the API in the near future.(However,
report
is still availableon GHC 7.10
and
on GHC master branch
as of 2015.)
Use
reportError
to report an error and carry on with theQ
computation(ultimately failing compilation anyway).
Use
fail
to stop with an error (GHC ≤ 7.10). That mightnot apply on GHC 8.0.