Elixir 中的条件保护子句

Mar*_*nto 1 elixir

有没有办法在函数保护子句中使用oror表达式:and

defmodule Test do
   def testfn(arg1, arg2) when is_nil(arg1) || is_nil(arg2), do: :nothing

   def testfn2(arg1, arg2) when is_nil(arg1) && is_nil(arg2), do: :nothing
end
Run Code Online (Sandbox Code Playgroud)

Dog*_*ert 5

Guard 表达式不支持&&and ||(接受 LHS 上的任何值),但仅and支持 and or(仅接受 LHS 上的布尔值)。由于is_nil始终返回布尔值,因此您可以切换到使用andand or

defmodule Test do
   def testfn(arg1, arg2) when is_nil(arg1) or is_nil(arg2), do: :nothing

   def testfn2(arg1, arg2) when is_nil(arg1) and is_nil(arg2), do: :nothing
end
Run Code Online (Sandbox Code Playgroud)

https://hexdocs.pm/elixir/master/guards.html包含守卫中允许的所有函数/运算符的列表。