olivierdevon
Is this expected behavior or am I missing something conceptually?
Erlang/OTP 24 [erts-12.1.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]
Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> l = %{a: 1}
%{a: 1}
iex(2)> %{a: 1} = %{a: 1, b: 2}
%{a: 1, b: 2}
iex(3)> ^l = %{a: 1, b: 2}
** (MatchError) no match of right hand side value: %{a: 1, b: 2}
I expected the last match to be successful. What misconception do I have?
I am not sure if this is addressed in any official documentation, but I couldn’t find any relevant discussion in the docs or the official guides.
Trending in Questions
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
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
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
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
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
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
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
eksperimental
You are trying to pattern match within the pin operator.
This is what is expected.
tomkonidas
I believe what it is trying to do is an exact match, so
^l = %{a: 1, b: 2}could be thought of like:ityonemo
Pinning match is always exact equality (otherwise there would be no way to match
%{}for exampleolivierdevon
I realized that it was performing an exact match. However, I cannot see why this is necessary. I thought that after the
i = %{a: 1}match and binding that^i = %{a: 1, b: 2}would involve a substitution of%{a: 1}for^iresulting an the match%{a: 1} = %{a: 1, b: 2}, which should succeed.Thank you for your reply.
olivierdevon
Thank you for your reply. I still do not understand why an exact match is needed. Why would there be no way to match
%{}?tomkonidas
In
%{a: 1} = %{a: 1, b: 2}We are matching the values inside the map.
ain this case, and since they are both1, it matches.In
^l = %{a: 1, b: 2}We are matching the entire map. Not only the k/v inside
olivierdevon
I am sorry to carry on the issue but I understand that the match is exact. My question is why is the semantics of
different from
even though
^lrefers to the value oflwhich is%{a: 1}?If this is the semantics by design, then I think it should be made explicit for novices like me as this is a bit confusing.
I can accept that the semantics is different but I don’t want to just know that they are semantically different I want to understand.
derek-zhou
Map matching is commonly used for destructuring a map, ie:
%{key1: value} = %{key1: 1, key2: 2}will bindvalueto 1. So this has to be a partial match.With a pin operator, there is no new binding of variable so it does not make sense to do partial match anymore.
Think this way, the partial match is a special case for convenience. The full match is the normal case.
ityonemo
to be fair, this is SUPER confusing because maps are the only datastructure which afford you inexplicit partial matching in elixir. Occasionally this causes problems when refactoring because in tests i often assert with matching instead asserting with equality.
in general, the map partial match
%{a: a} = some_mapshould be thought of as the (“not legal code”)%{a: a, ...} = some_map@olivierdevon in light of this, one way to explain is that from an experienced elixir dev’s pov, what you are expecting to happen is that the pin operator matching is a lexical match against the variable, but that’s not possible because in general lexical content is thrown away once the variable is assigned.
LostKobrakai
The mismatch here is that the runtime value of
%{a: 1}is not a pattern to match against. Patterns on the beam are not a datatype, but they’re compiled from code. You can only hardcode patterns (or do metaprograming on their AST), but you cannot create pattern matches from runtime values.Patterns not being higher order is a limitation of the beam. This is the reason why a pinned variable can only ever be compared to be equal, but not be used as a pattern.