theodore

theodore

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

Showing Posts 1 to 8

voughtdq

voughtdq

AFAIK It’s just a stylistic choice.

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 #2
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 #3
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.

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”.

derek-zhou

derek-zhou

Most Erlang code I’ve seen use the right hand side though. Pattern matching in the function haed has 2 purposes:

  • to de-structure the parameter. Left hand side feels more natual for this purpose.
  • to conditionally match one function clause out of several. Right hand side feels more natual for this purpose.

I guess Erlang and Elixir just pick different style convention out of the emphasizing which of the purposes are more important.

sodapopcan

sodapopcan

I think part of what makes it make sense in Erlang is that it doesn’t have re-binding. I feel like it would almost make more sense in Elixir if you had to use a pin to make if you want to put it on the left: def foo(^user = %User{}) since it’s sorta like, “this already has a value”. To be clear, I’m not suggesting that’s how I think it should actually be.

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews