【发布时间】:2018-08-23 16:44:36
【问题描述】:
我正在尝试使我的库 (https://github.com/CrowdHailer/OK) 用于处理结果元组与透析器很好地配合使用。
如果输入不正确的数据,现在有代码会引发一个很好的错误。然而,dialyzer 指出,这种情况是不需要的。我仍然想保留这个错误案例。向图书馆的新用户解释错误非常有帮助,但我想让透析器忽略它。
quote location: :keep do
case unquote(right) do
{:ok, unquote(left)} ->
unquote(expand_bindings(rest, yield_block, exception_clauses))
{:error, reason} ->
{:error, reason}
case reason do
unquote(exception_clauses)
end
# This block will never be called, as far as dialyzer is concerned
# However I want to keep it for the cases when diayzer is not being used in the project
return ->
raise %OK.BindError{
return: return,
lhs: unquote(Macro.to_string(left)),
rhs: unquote(Macro.to_string(right))
}
end
end
源码可以看这里https://github.com/CrowdHailer/OK/blob/431142204794e1702271c86d6594ce76b8978b57/lib/ok.ex#L512-L529
【问题讨论】:
-
向图书馆的新用户解释错误非常有帮助——这就是 cmets/@doc 的用途。出于文档目的,在代码中保留 unreachable 子句是没有意义的。而且......那个内部案例陈述是什么?是伪代码吗?
-
代码是可访问的,以防您违反规范。但经验表明,许多 Elixir 开发人员会以不符合规范的方式使用它。如果不运行透析器,我的错误将是他们看到的第一件事
-
关于您的评论“代码是可访问的,以防您违反规范”,可能值得使用
dialyzer --no_spec验证您的直觉。