我可以使用警卫来测试参数是否为true
:
defmodule Truth do
def true?(term) when term, do: "#{term} is true"
def true?(term), do: "#{term} is not true"
end
这对布尔值按预期工作:
Truth.true?(true)
#=> "true is true"
Truth.true?(false)
#=> "false is not true"
但它无法测试其真实性:
Truth.true?(1)
#=> "1 is not true"
是否可以测试警卫的真实性?例如,下面的函数可以使用上述风格的守卫来编写true?/1
吗?
def truthy?(term) do
if term, do: "#{term} is truthy", else: "#{term} is falsey"
end