theodore

theodore

Why do most functions pattern match structs on the left hand side?

Noob question. But I was just wondering, is there any advantage to pattern matching a struct on the left hand side vs right hand side?

For e.g

defmodule Test do
  defstruct name: nil

  def fun1(x = %Test{name: name}) do
    IO.inspect(name)
  end

  def fun2(%Test{name: name} = x) do
    IO.inspect(name)
  end
end

It seems like both are equivalent, but the style of “struct on left hand side” is more common.

Here’s some other instances of what I mean

Most Liked

codeanpeace

codeanpeace

Another reason why it’s likely more common is that it keeps things consistent.

When pattern matching outside a function head, the variable is always on the right of the = match operator since the other way around is for variable assignment

defmodule Test do
  defstruct name: nil

  def fun(x) do
    %Test{name: name} = x
    IO.inspect(name)
  end
end
13
Post #4
kip

kip

ex_cldr Core Team

You are right, they are functionally equivalent.

I tend to use the left hand side because when I’m scanning code, its the pattern match I want to focus on first. The binding is the second consideration - especially when there are multiple function heads matching on different constructs.

10
Post #3
rvirding

rvirding

Creator of Erlang

Just to be different I prefer in Erlang to use the left hand side. To me it looks more like a pattern match which is what I am doing. :smile:

al2o3cr

al2o3cr

+1 to this.

One other consideration: the x = %Test{name: name} shape looks similar to how other languages declare default arguments, versus the struct-on-the-left one which always means “pattern-match”.

adamu

adamu

This is the best reason I think :slight_smile:

More specifically, the variables on the left side get bound (unless you use the pin operator to indicate you want to use an existing variable) based on the data on the right. In a function head, the variables on both sides are bound based on the argument to the function.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New

We're in Beta

About us Mission Statement