简体   繁体   English

在Elixir规范定义中使用guard子句时,看到“ spec has a arity错误”

[英]“spec has wrong arity” seen when using guard clause for Elixir spec definition

Here is the code I am trying to add a spec to. 这是我要添加规范的代码。

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

The first spec definition works fine. 第一个规范定义可以正常工作。 However I want to make it clearer that if reason is an integer then the returned error will also contain an integer. 但是,我想更清楚一点,如果原因是整数,那么返回的错误也将包含一个整数。 The second spec doesn't work. 第二个规范不起作用。 the error is 错误是

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

The code you provided perfectly works with functions (as opposite to macros.) Even the Elixir testsuite for typespecs has only functions tests with guards in specs . 您提供的代码可以完美地与函数配合使用(与宏相反)。即使是用于typespec的Elixir测试套件,也只能在spec中使用带有防护的功能测试

I cannot tell if this is not yet implemented or it's a bug or what. 我无法确定这是否尚未实现,或者是错误还是什么。 I believe you'd better ask the question directly in Elixir maillist and/or on Elixir forum . 我相信您最好直接在Elixir邮件列表和/或Elixir论坛中提问。 Or. 要么。 maybe it's worth it to file an issue. 也许值得提出一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM