ConstantBall
We all know that we’re able to build a map using the literal syntax using %{key1 => value1, key2 => value2}, as well as update existing keys of a map with %{map | key2 => new_value2}. But what reason could there be for not allowing us to insert new key-value pairs into a map using the literal syntax?
Inserting and updating are both supported (simultaneous) operations in Erlang using the literal syntax where the update is denoted by the := operator and insertion by the => operator.
So what is stopping Elixir from having something analogous?
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
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
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
- #ai
- #elixirconf-us
- #blog-post
- #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)
adamu
I don’t know, but how do you think this should look in Elixir?
josevalim
I frequently struggle to remember when to use which operator in the Erlang syntax. So my goal was to introduce syntax for the struct-like API (i.e. you expect the keys exist and you can only update them) and leave the dictionary-like API (adding and removing keys) in the
Mapmodule. So you can doMap.put/3to add,Map.delete/2and so on.Eiji
If we are in topic was there a discussion to allow
insertof map keys withinupdatestruct syntax? That may be a good resource to read …josevalim
If there was, it was a long time ago, before 1.0, on the elixir-lang-core mailing list.
ConstantBall
I honestly can’t tell you, although I also don’t think it’s relevant here. Half of that is already implemented in Elixir today, namely
Map#{a := update}as%{map | a => :update}, so I don’t see that as being the blocker. I’m just trying to understand why something analogous toMap#{ b => new}doesn’t exist in Elixir.ConstantBall
But there isn’t anything specific about the update syntax which makes it exclusively for structs since you’re able to update maps whose keys are of any type.
josevalim
Yes, exactly. Which is why I said “struct-like” but indeed, it works for regular maps too.
ConstantBall
So I guess I still don’t understand why that syntax isn’t included. It sounds like you’re saying the reason for Elixir not having the map literal insert syntax is because your thought was that insertions are more aligned with dictionary operations and thus should only be done through the
Mapmodule. Yet the update syntax is allowed, which is counterintuitive to me because I would think if one is supported then the other should be as well due to the close relationship between the two operations, no? Otherwise, why not restrict its use to only structs where insertions are invalid?derek-zhou
An Elixir struct is a map with a known set of atom keys. For a struct, an insert would not make much sense, but updates are very common. I don’t know about others, but I only use the shorthand update syntax for structs, ie:
%{struct | field: value}, For plain maps, I always use theMap.put/3,Elixir uses
:for atom keys for developer ergonomics.If Elixir need to support both insert and update in shorthand syntax, there will be 2*2=4 different notations, which would be hard to remember.ConstantBall
If you attempt to update a field which doesn’t exist in a struct using the update syntax, an exception will be thrown. I don’t see why that logic couldn’t be extended when attempting to insert a key into a struct if the syntax were supported.
Whether it is a struct or map, I always prefer the update syntax if I know the key must exist. I want to know if the state is contrary to what I expect and
Map.putdoesn’t allow for that. I believe thatputshould strictly be used for inserting.Why would a second operator automatically double the number of notations? Why couldn’t it just add one more notation?
Also, for the purposes of this discussion, I’m not concerned with the implementation details. I’m only interested in understanding how Elixir came to the conclusion to omit the insert syntax.