谁能解释一下为什么这不起作用?
当您调用函数时,elixir 从定义中的第一个函数子句开始,并尝试将函数调用中指定的参数与函数定义中的参数匹配。如果没有匹配,elixir 然后尝试下一个函数子句。当找到匹配项时,执行相应的函数体。如果没有一个函数子句匹配,那么你会得到一个 function_clause 错误。
让我们看一个例子。如果你写:
def go({1, 2}, :cat) do
IO.puts "I must have matched the function call."
end
然后你可以像这样调用那个函数:
iex(2)> A.go({1, 2}, :cat)
I must have matched the function call.
:ok
但是,如果你尝试:
ex(3)> A.go("hello", 10)
** (FunctionClauseError) no function clause matching in A.go/2
The following arguments were given to A.go/2:
# 1
"hello"
# 2
10
a.ex:2: A.go/2
你得到一个函数子句错误,因为go()的定义没有函数子句匹配函数调用:
function call: A.go("hello", 10)
| |
{1,2} = "hello | | :cat = 10
V V
function def: def go({1, 2}, :cat) do
go()函数调用中唯一能匹配函数定义中元组{1, 2}的参数是元组{1, 2},函数调用中唯一能匹配函数定义中原子:cat的参数函数定义是原子:cat。
但是,函数定义中的变量可以匹配任何内容。合法变量名称的一些示例是:x、y、_x 和 _。所以,如果你这样定义go():
def go(_x, _y) do
IO.puts "I must have matched the function call."
end
那么两个函数调用都会匹配:
iex(3)> c "a.ex"
warning: redefining module A (current version defined in memory)
a.ex:1
[A]
iex(4)> A.go("hello", 10)
I must have matched the function call.
:ok
iex(5)> A.go({1, 2}, :cat)
I must have matched the function call.
:ok
如果在 go() 定义中添加第二个函数子句:
def go(_x, _y) do
IO.puts "I must have matched the function call."
end
def go(_, _) do
IO.puts "I will never match"
end
第一个函数子句仍然会匹配任意两个参数,因为这样,第二个函数子句将永远不会执行——所以编译器会给你一个警告,相当于:
什么?你为什么在第二个函数子句中乱写所有垃圾
什么时候它永远不会匹配,因此永远不会执行?!抓紧!
当您在函数定义中进行模式匹配时,事情会变得有些棘手,例如:
def go(%{a: val}=map, x) do
请参阅 here 了解其工作原理。