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
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
voughtdq
AFAIK It’s just a stylistic choice.
kip
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.
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 assignmentadamu
This is the best reason I think
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
+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
Most Erlang code I’ve seen use the right hand side though. Pattern matching in the function haed has 2 purposes:
I guess Erlang and Elixir just pick different style convention out of the emphasizing which of the purposes are more important.
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
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.