【问题标题】:"spec has wrong arity" seen when using guard clause for Elixir spec definition在 Elixir 规范定义中使用保护子句时看到“规范有错误的数量”
【发布时间】:2018-08-22 16:52:17
【问题描述】:

这是我尝试添加规范的代码。

@spec failure(term) :: error(term)
@spec failure(reason) :: error(reason) when reason: term
defmacro failure(reason) do
  quote do
    {:error, unquote(reason)}
  end
end

第一个规范定义工作正常。 但是我想更清楚一点,如果原因是一个整数,那么返回的错误也将包含一个整数。 第二个规范不起作用。错误是

Compiling 1 file (.ex)

== Compilation error in file lib/ok.ex ==
** (CompileError) lib/ok.ex:71: spec has wrong arity
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:677: :erl_eval.do_apply/6

【问题讨论】:

    标签: elixir dialyzer typespec


    【解决方案1】:

    您提供的代码与函数完美配合(与宏相反)。甚至用于 typespecs 的 Elixir 测试套件也有 only functions tests with guards in specs

    我不知道这是否还没有实现,或者它是一个错误还是什么。我相信你最好直接在 Elixir 邮件列表和/或Elixir forum 上提问。或者。也许值得提出问题。

    【讨论】:

    • 我认为这是一个比 Elixir 论坛更好的帮助问题的论坛。如果这是一个问题,那么我可以打开它。目前不在邮件列表中。
    • 嗯,它看起来像个问题,它嘎嘎作响。它适用于def,不适用于defmacro。那不应该是AFAICT的情况。我推荐了 Elixir 论坛,因为 José 会阅读它。
    猜你喜欢
    • 2017-10-10
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多