Peter Saxton
Peter Saxton

Reputation: 4666

"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

Upvotes: 0

Views: 123

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

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.

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. Or. maybe it’s worth it to file an issue.

Upvotes: 0

Related Questions