【问题标题】:How to override a function or variable type in Elixir and Dialyzer?如何在 Elixir 和 Dialyzer 中覆盖函数或变量类型?
【发布时间】:2020-05-14 03:18:44
【问题描述】:

我正在使用 Elixir,但出现 Dialyzer(通过 Dialyxir)错误提示

The pattern
variableVdate

can never match, because previous clauses completely cover the type
{:error, :badarg}.

这里是代码

date = Timex.DateTime.from_seconds(0)

case date do
  {:error, :badarg} ->
    {:error, "Bad Date"}

  date ->
    {:ok, date}
end

我相信这是因为Timex.DateTime.from_seconds 有一个incorrect type spec

他们将其定义为

@spec from_seconds(non_neg_integer) :: DateTime.t :: {:error, atom}

但我认为应该是

@spec from_seconds(non_neg_integer) :: DateTime.t | {:error, atom}

是否可以通过某种方式覆盖类型规范或date 类型来解决此问题?

由于其他原因,我无法将 Timex 升级到版本 3。

【问题讨论】:

    标签: elixir dialyzer timex


    【解决方案1】:

    我认为这是因为您使用特定值 0 调用 from_seconds/1,因此返回不是可变的。 我的猜测是在几秒钟内运行它,因为传入的变量可能会起作用:

    case Timex.DateTime.from_seconds(seconds) do
      {:error, :badarg} ->
        {:error, "Bad Date"}
    
      date ->
        {:ok, date}
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      相关资源
      最近更新 更多