如何避免协议出现Dialyzer错误?

问题描述:

一个简单的协议收益率2种透析器警告:如何避免协议出现Dialyzer错误?

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 
+0

你可以看看,看看这个最近创建的bug是否有关系吗? http://bugs.erlang.org/browse/ERL-159 – aronisstav

+0

@aronisstav这是erlang 19中的一个回归。我在18岁时看到了这一点,所以可能不会。 –

我使用

@dialyzer {:nowarn_function, __protocol__: 1} 

在协议定义了。

+0

谢谢!我会试一试。这是否完全解决了这个问题? – Dogweather

+0

当我尝试它时失败:'{{nocatch,{error,“未知函数'__protocol __'/ line in lib/dtest.ex:1}}' – Dogweather

+0

Ah hah,so ** within the'the defprotocol '。 – Dogweather