灵药中的条件保护条款

时间:2017-06-08 21:28:12

标签: elixir

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

and

1 个答案:

答案 0 :(得分:4)

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

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

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