0

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
Peter Saxton
  • 4,466
  • 5
  • 33
  • 51

1 Answers1

0

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.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • I thought that this was a better forum, than the Elixir forum, for help questions. If it is an issue then I can open that. Not currently on the mailing list. – Peter Saxton Aug 22 '18 at 13:01
  • Well, it walks like an issue and it quacks like an issue. It works with `def` and does not work with `defmacro`. That should not be the case AFAICT. I suggested Elixir forum because José reads it. – Aleksei Matiushkin Aug 22 '18 at 13:10