我正在使用 elixir_talk 库。连接后,我想在连接到 beanstalkd 后调用一个私有函数。我刚刚添加了 typespecs 并运行了 Dialyzer(通过 dialyxir)。我得到错误:
my_module.ex:3: The specification for 'Elixir.MyModule':f/0 states that the function might also return 'ok' | {'error',_} but the inferred return is none()
my_module.ex:4: Function f/0 has no local return
my_module.ex:14: Function g/1 will never be called
我能找到的产生这个的最小例子是
defmodule MyModule do
@spec f() :: :ok | {:error, term}
def f() do
case ElixirTalk.connect('127.0.0.1', 11300) do
{:ok, conn} ->
g(conn)
{:error, err} ->
{:error, err}
end
end
@spec g(pid) :: :ok
defp g(pid) do
:ok
end
end
如果我将调用替换为调用ElixirTalk.connect
,spawn
Dialyzer 将不再报告任何问题。
defmodule MyModule do
@spec f() :: :ok
def f() do
x = spawn fn -> :done end
g(x)
end
@spec g(pid) :: :ok
defp g(pid) do
:ok
end
end
有谁知道为什么 Dialyzer 在这里感到困惑?