4

一个简单的协议会产生两种透析器警告:

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

(ETC。)

有没有@spec可以和defprotocol's一起使用的?我没有找到任何例子。或者,有没有办法在源代码中标记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
4

1 回答 1

3

我在用着

  @dialyzer {:nowarn_function, __protocol__: 1}

现在在协议定义中。

于 2016-06-17T01:11:41.687 回答