Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we cant use other functions in Elixir guard clauses or macros?

Tags:

elixir

The code below shows an example of what I'm asking about: why Map, Enum, etc... can't be used within guard clauses or inside my own macros.

defmodule GuardMod do
    defmacro has_any_key(arg, keys) do
        quote do
            Map.keys(unquote(arg), unquote(keys)) |> Enum.any?
        end
    end
end

defmodule OtherMod do
    import GuardMod

    @doc """
    has_any_key/2 is allowed, but Map.keys or Map.any not
    """
    def fn1(list) when has_any_key(list, [:key1, :key2]), do: :nothing
end
like image 205
Marcos Costa Pinto Avatar asked Mar 08 '23 21:03

Marcos Costa Pinto


1 Answers

It's a compromise to allow multi function head dispatching to be "fast".

The type of functions allowed in guards all have a limited runtime or reduction count. If you allow arbitrary functions in guards, you have to deal with the case in which the guard evaluation does not finish before the current process exceeds it current execution slice. ( Or potentially even worse cases a race condition because it requires input from another process).

like image 127
Fred the Magic Wonder Dog Avatar answered Apr 08 '23 05:04

Fred the Magic Wonder Dog