While it’s impossible per se, one might slightly improve the performance by generating the clauses when possible integer values are known in advance. Also, Kernel.map_size/1, which is allowed in guards, will be required:
defmodule NoIntegerKeys do
defmacro __using__(name: name, disallowed: enum, do: block) do
[
(quote do: def unquote(name)(%{} = map) when map_size(map) == 0, do: :ok) |
Enum.map(enum, fn i ->
quote do
def unquote(name)(%{unquote(i) => _}), do: :error
end
end)
] ++ [
(quote do: def unquote(name)(%{} = map), do: unquote(block))
]
end
end
defmodule Test do
use NoIntegerKeys, name: :f1, disallowed: [0,1,2], do: :ok
end
Test.f1(%{})
#⇒ :ok
Test.f1(%{foo: :bar})
#⇒ :ok
Test.f1(%{:foo => :bar, 3 => :baz})
#⇒ :ok
# BUT
Test.f1(%{:foo => :bar, 2 => :baz})
#⇒ :error
This example is a bit contrived, but it shows how one fight for the performance when it’s really needed. Macros are expanded by the compiler, so the resulting code would declare 5 different clauses: one for an empty map, three for possible values and the last one for the default execution block.