我有一些代码使透析器失败,我不明白为什么。无论我@spec
在函数顶部放入什么,对该函数的调用都会返回一个令人费解的透析器错误。这是函数的简化。据我所知,我已经正确指定了该功能。
@spec balances(uuid :: String.t(), retries :: non_neg_integer) ::
{:ok, list()}
| {:internal_server_error, String.t(), String.t()}
| {:internal_server_error, map | list, String.t()}
def balances(uuid, retries \\ 0) do
url = "/url/for/balances" |> process_url
case HTTPoison.get(
url,
[, {"Content-Type", "application/json"}],
[]
) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
response = Poison.decode!(body, as: %{"message" => [%Currency{}]})
cond response["message"] do
length(bal) > 0 ->
{:ok, bal}
retries >= 1 ->
{:ok, []}
true ->
init(uuid)
balances(uuid, retries + 1)
end
{:error, %HTTPoison.Error{reason: reason}} ->
Notifier.notify(url, reason, Helpers.line_info(__ENV__))
{:internal_server_error, reason, url}
{_, %HTTPoison.Response{body: body} = res} ->
response = Poison.decode!(body)
Notifier.notify(url, response, Helpers.line_info(__ENV__))
{:internal_server_error, response, url}
end
end
我的问题是,如果我希望得到除以下内容之外的任何内容,则跨代码库对该函数的每次调用都会失败{:ok, balances}
:
user_balances =
case balances(uuid) do
{:ok, user_balances} -> user_balances
_ -> [] # Dialyzer error here
end
透析器警告说The variable _ can never match since previous clauses completely covered the type {'ok',[map()]}
。我读到这意味着对 balances 的任何调用都将始终 return {:ok, balances}
,但这不可能是真的,因为 case 语句 forHTTPoison.get
是函数中评估的最后一件事,它似乎只有三个可能的结果:
{:ok, list}
{:internal_server_error, String.t(), String.t()}
{:internal_server_error, map | list, String.t()}
.
我知道我可能遗漏了一些非常明显的东西,但我无法弄清楚它是什么。任何帮助将不胜感激。谢谢你!