spammy

spammy

In which direction does match operator binding occur?

Consider this reduced example from Exercism. Given that binding[1] only happens from right of the match operator to left of it, why does the binding work in the function head here?

defmodule Example do
  def named_function(:a = variable_a) do
    {variable_a, 1}
  end
end

Example.named_function(:a)
# => {:a, 1}

Most Liked

rvirding

rvirding

Creator of Erlang

“Matching” in patterns is not really the same as matching in a body. It is actually based on ‘aliases’ in Erlang where the rationale is that both sides of the = must match the argument and that they really have nothing to do with each other. Also all variables in the patterns are usable afterwards. That is why a common use is to both test what an argument should look and get a reference to the whole structure:

def foo({;a, :b, x, y, z} = all_of_it) when x == y + z do
use(all_of_it) %We know all_of_it has the right format
end

Or to be a bit fun

def foo({a,b,c} = {x,y,z}) do
use_1(a, b, c) == use_1(x, y, z)
end

LostKobrakai

LostKobrakai

For your mental model parameter matching works like this:

:a = variable_a = parameter_value
# with explicit parenthesis (right associativity)
:a = (variable_a = parameter_value)

So the parameter value is first matched with variable_a. The match operator always returns the right hand side value, so afterwards :a = :a is evaluated, which doesn’t raise and again return the right hand side.

Given those behaviours order in the function head essentially doesn’t matter much, though having more constraining match values more to the right could be a micro optimization.

raulrpearson

raulrpearson

The Erlang docs helped me clarify some nuance, assuming these things map onto Elixir:

The = character is used to denote two similar but distinct operators: the match operator and the compound pattern operator. Which one is meant is determined by context.

A match expression takes the form Pattern = Expr:

If the matching succeeds, any unbound variable in the pattern becomes bound and the value of Expr is returned. If multiple match operators are applied in sequence, they will be evaluated from right to left.

A pattern is just a term that can contain unbound variables. A compound pattern is of the form Pattern = Pattern:

The compound pattern operator is used to construct a compound pattern from two patterns. Compound patterns are accepted everywhere a pattern is accepted. A compound pattern matches if all of its constituent patterns match. It is not legal for a pattern that is part of a compound pattern to use variables (as keys in map patterns or sizes in binary patterns) bound in other sub patterns of the same compound pattern.

Also:

The compound pattern operator does not imply that its operands are matched in any particular order. That means that it is not legal to bind a variable in Pattern1 and use it in Pattern2, or vice versa.

When we declare a function, the arguments are actually treated as patterns (or compound patterns) that will be matched against the expression supplied when calling the function.

I don’t know if “does not imply that its operands are matched in any particular order” holds when invoking functions in Elixir and matching the supplied expression to the declared pattern, though.


The distinction between a pattern (a term that can contain unbound variables) and other types of expressions and understanding where each can be used made it click for me I think.

When you specify an argument in a function declaration or write the left-hand side of a case, you’re writing patterns and compound patterns, not match expressions.

Matching and binding happens in match expressions. When you use = where a pattern is expected, you’re just writing a compound pattern.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement