What is the equal sign in a function parameter in Elixir?

I defined the module Fooas follows:

defmodule Foo do
  def hello(x = %{name: name}) do
    IO.inspect [x, name]
  end
end

If I run Foo.hello(%{name: "Alice"}), I get the following result:

[%{name: "Alice"}, "Alice"]

Then I found out that I can rewrite a module Foowithout changing its functionality as follows:

defmodule Foo do
  def hello(%{name: name} = x) do
    IO.inspect [x, name]
  end
end

Why is this possible? What is an equal sign in a function parameter? Is this a regular match operator?

In my understanding, the operator =corresponds to the value on the right side relative to the picture on the left side.

[change]

After reading Justin's answer, I sent the answer myself. But I still need help.

I want to know if the operator =performs functions differently in the head and why.

And I want to find official documentation, if available.

+4
2

.

, , - ,

iex(1)> 1 = x

** (CompileError) iex:3: undefined function x/0
iex(2)> x = 1
1
iex(3)> 1 = x
1

x , , ( def hello(x) do ... end). , .

+2

http://learnyousomeerlang.com/syntax-in-functions.

Erlang fuction:

valid_time({Date = {Y,M,D}, Time = {H,Min,S}}) ->
  io:format("The Date tuple (~p) says today is: ~p/~p/~p,~n",[Date,Y,M,D]),
  io:format("The time tuple (~p) indicates: ~p:~p:~p.~n", [Time,H,Min,S]);

:

, =, ({Y,M,D}) (Date).

, , , .

Foo :

defmodule Foo do
  def hello(%{name: x} = %{name: y}) do
    IO.inspect [x, y]
  end
end

Foo.hello(%{name: "Alice"}) :

["Alice", "Alice"]

=.

0

Source: https://habr.com/ru/post/1668397/


All Articles