如何避免协议的透析器错误?
How to avoid Dialyzer errors for protocols?
一个简单的协议会产生两种透析器警告:
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" 列出:
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
我正在使用
@dialyzer {:nowarn_function, __protocol__: 1}
目前在协议定义中。
一个简单的协议会产生两种透析器警告:
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" 列出:
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
我正在使用
@dialyzer {:nowarn_function, __protocol__: 1}
目前在协议定义中。