要匹配地图的一些关键,也是整个地图存储在一个变量,你可以使用= variable
与图案:
def check_data(arg1, %{"action" => "action1"} = map, arg2) do
end
此功能将匹配任何包含在关键"action"
的"action1"
地图(和任何其它的键/值对)作为第二参数,并且整个地图存储在map
:
iex(1)> defmodule Main do
...(1)> def check_data(_arg1, %{"action" => "action1"} = map, _arg2), do: map
...(1)> end
iex(2)> Main.check_data :foo, %{}, :bar
** (FunctionClauseError) no function clause matching in Main.check_data/3
iex:2: Main.check_data(:foo, %{}, :bar)
iex(2)> Main.check_data :foo, %{"action" => "action1"}, :bar
%{"action" => "action1"}
iex(3)> Main.check_data :foo, %{"action" => "action1", :foo => :bar}, :bar
%{:foo => :bar, "action" => "action1"}
你是指像'DEF check_data(%{ “动作”=> “动作1”} = ARG1,ARG2) '或'def check_data(arg1,%{“action”=>“acti on1“} = arg2)'? – Dogbert
@Dogbert不,不。我的意思是说除地图外还有其他的论点。我们可以在函数参数中使用'arg1 =%{}'吗? :O – dotslash
因此'def check_data(arg1,%{“action”=>“action1”} = map,arg2)'?是的,你可以在函数参数中使用'='。 – Dogbert