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