如果可以的话,我怎么能用"或"条件模式匹配?我需要这个,因为我有一些不同的条件,哪些动作是相同的?
case something123 do
:a -> func1()
:b -> func1()
:c -> func1()
:d -> func2()
end
Run Code Online (Sandbox Code Playgroud)
Dog*_*ert 15
你可以使用in和列出:
case something123 do
x when x in [:a, :b, :c] -> func1()
:d -> func2()
end
Run Code Online (Sandbox Code Playgroud)
x in [:a, :b, :c]扩展到x == :a or x == :b or x == :c当in宏检测到它正在从一个后卫语句调用,RHS是一个文字列表(因为你不能从卫兵调用远程功能).
您也可以使用cond来做到这一点。
cond do
something123 == :a or something123 == :b something123 == :c ->
func1()
something123 == :d ->
func2()
end
Run Code Online (Sandbox Code Playgroud)