spammy
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}
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
For your mental model parameter matching works like this:
So the parameter value is first matched with
variable_a. The match operator always returns the right hand side value, so afterwards:a = :ais 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.
spammy
Great, thank you. I tried reversing the expression, (ie
def named_function(variable_a = :a) do), and it seemed to work in the same way. Reasoning it through,variable_a = (:a = parameter_value)seems to make more sense from a “function selection” point of view, so why is the example in the original post preferred (if at all)?LostKobrakai
I‘d not sweat that. Use the order, which makes more sense to you or people working with the codebase.
rvirding
“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 douse(all_of_it) %We know all_of_it has the right formatendOr to be a bit fun
def foo({a,b,c} = {x,y,z}) douse_1(a, b, c) == use_1(x, y, z)endadamu
It looks like Elixir’s docs are lacking an explanation for this behaviour.
For example, the Pattern Matching guide says:
but does not cover the case of function parameter binding.
Additionally the Modules and Functions guide does not cover pattern matching in function parameters.
Is it accurate to say that for functions, each argument is bound to the entire expression for each parameter (déjà vu, I’m sure I’ve read this somewhere…)?
You can see here that
a,a2andbwere bound from the argument, regardless of which side of which=they are.raulrpearson
The Erlang docs helped me clarify some nuance, assuming these things map onto Elixir:
A match expression takes the form
Pattern = Expr:A pattern is just a term that can contain unbound variables. A compound pattern is of the form
Pattern = Pattern:Also:
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.