条件

cond do 
   0 == 1 -> IO.puts "0 = 1"
   2 == 1 + 1 -> IO.puts "1 + 1 = 2" 
   3 == 1 + 2 -> IO.puts "1 + 2 = 3" 
end

# Outputs "1 + 1 = 2" (first condition evaluating to true)

如果没有条件,cond 将提升一个 CondClauseError

cond do
  1 == 2 -> "Hmmm"
  "foo" == "bar" -> "What?"
end
# Error

通过添加始终为真的条件可以避免这种情况。

cond do
  ... other conditions
  true -> "Default value"
end

除非预计永远不会达到默认情况,并且程序实际上应该在那时崩溃。