【发布时间】:2016-06-06 23:54:34
【问题描述】:
一个简单的协议会产生两种透析器警告:
defmodule Dtest do
defprotocol Valid do
@doc "Returns true if data is in a valid state"
def valid?(data)
end
defimpl Valid, for: Integer do
def valid?(_), do: true
end
end
我想不通的警告是这样的:
dtest.ex:2: The specification for
'Elixir.Dtest.Valid':'__protocol__'/1 states that the function might
also return 'true' but the inferred return is
'Elixir.Dtest.Valid' | 'false' | [{'valid?',1},...]
我也想不出一个 @spec 可以在这里消除警告。
另一种警告已在别处讨论过——列出了许多“未知功能”:
Unknown functions:
'Elixir.Dtest.Valid.Atom':'__impl__'/1
'Elixir.Dtest.Valid.BitString':'__impl__'/1
(等等)
是否有可以与defprotocol 一起使用的@spec?我没有找到任何例子。或者,有没有办法在源代码中将defprotocol 标记为被透析器忽略?
编辑:这是第一个错误的完整修复:
defmodule Dtest do
defprotocol Valid do
@doc "Returns true if data is in a valid state"
@dialyzer {:nowarn_function, __protocol__: 1}
def valid?(data)
end
defimpl Valid, for: Integer do
def valid?(_), do: true
end
end
【问题讨论】:
-
你能看一下这个最近创建的错误是否相关? bugs.erlang.org/browse/ERL-159
-
@aronisstav 这是 erlang 19 中的回归。我在 18 中看到了这个,所以可能没有。