【问题标题】:How to avoid Dialyzer errors for protocols?如何避免协议的透析器错误?
【发布时间】: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 中看到了这个,所以可能没有。

标签: protocols elixir dialyzer


【解决方案1】:

我正在使用

  @dialyzer {:nowarn_function, __protocol__: 1}

暂时在协议定义中。

【讨论】:

  • 谢谢!我会试一试。这能完全解决问题吗?
  • 尝试失败:{{nocatch,{error," Unknown function '__protocol__'/1 in line lib/dtest.ex:1"}}
  • 啊哈哈,所以defprotocol
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
  • 2019-11-01
  • 1970-01-01
  • 2012-10-06
相关资源
最近更新 更多