olivierdevon

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.

Showing Posts 1 to 10

eksperimental

eksperimental

You are trying to pattern match within the pin operator.

This is what is expected.

iex> ^l = %{a: 1} 
%{a: 1}

tomkonidas

tomkonidas

I believe what it is trying to do is an exact match, so ^l = %{a: 1, b: 2} could be thought of like:

%{a: 1} == %{a: 1, b: 2}
ityonemo

ityonemo

Pinning match is always exact equality (otherwise there would be no way to match %{} for example

olivierdevon

olivierdevon OP

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 ^i resulting an the match %{a: 1} = %{a: 1, b: 2}, which should succeed.

Thank you for your reply.

olivierdevon

olivierdevon OP

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

tomkonidas

In %{a: 1} = %{a: 1, b: 2}
We are matching the values inside the map. a in this case, and since they are both 1, it matches.

In ^l = %{a: 1, b: 2}
We are matching the entire map. Not only the k/v inside

olivierdevon

olivierdevon OP

I am sorry to carry on the issue but I understand that the match is exact. My question is why is the semantics of

^l = %{a: 1, b: 2}

different from

%{a: 1} = %{a: 1, b: 2}

even though ^l refers to the value of l which 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

derek-zhou

Map matching is commonly used for destructuring a map, ie: %{key1: value} = %{key1: 1, key2: 2} will bind value to 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

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_map should 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

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.

Where Next? Top

Trending in Questions Top

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
ryanwinchester
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 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
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
Dmk
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
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