下面的代码显示了我所询问的一个示例:为什么 Map、Enum 等...不能在保护子句或我自己的宏中使用。
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
Run Code Online (Sandbox Code Playgroud)
允许多功能头部调度“快速”是一种妥协。
守卫中允许的函数类型都有有限的运行时间或减少计数。如果你在保护中允许任意函数,你必须处理在当前进程超过当前执行切片之前保护评估没有完成的情况。(或者可能更糟糕的情况是竞争条件,因为它需要来自另一个进程的输入)。