Is %{...} not legal typespec shorthand for any map?


In the Typespecs docs, there is a reference to the literal %{…} as shorthand for any map. I have not been able to get this to compile though. This is the error I get:

== Compilation error on file web/models/user.ex ==
** (FunctionClauseError) no function clause matching in anonymous fn/1 in Macro.map_list_to_string/2
    (elixir) lib/macro.ex:904: anonymous fn({:..., [line: 15], nil}) in Macro.map_list_to_string/2
    (elixir) lib/enum.ex:1215: anonymous fn/4 in Enum.map_join/3
    (elixir) lib/enum.ex:1623: Enum."-reduce/3-lists^foldl/2-0-"/3
    (elixir) lib/enum.ex:1214: Enum.map_join/3
    (elixir) lib/macro.ex:609: Macro.to_string/2
    (elixir) lib/kernel/typespec.ex:773: anonymous fn/5 in Kernel.Typespec.typespec/3
    (stdlib) lists.erl:1239:
    (elixir) lib/kernel/typespec.ex:752: Kernel.Typespec.typespec/3
    (elixir) lib/kernel/typespec.ex:390: Kernel.Typespec.translate_type/3
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:116: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

In user.ex, line 15:
@type params :: %{...}

I’m running Elixir 1.3.2.

Am I misreading the usage for this literal?


1 Like

As far as I remember the meaning of %{} and %{...} does depend on the OTP version backing your elixir.

1 Like

I am running Erlang/OTP 19

1 Like

I believe (But tell me if I’m wrong) this is a difference between the Typespec-syntax used inside @type, @spec and @callback and normal pattern-matching used in most other places.

As far as I know, normal pattern-matching still works with %{} == any map, even when using Elixir on OTP 19.

1 Like

The typespec map() should work to match any map on any map-containing OTP version.

1 Like