zacky1972

zacky1972

Creator of Pelemay

Do you know any resourses that describe the reason why options came to be represented by a keyword list instead of a map?

Hi,

I’m asked the reason why options came to be represented by a keyword list instead of a map, but I don’t know this in detail, so I’m searching some resources or evidences that describe this.

If there is something that seems to be a clue, even if it is trivial, please tell me about it.

Most Liked

c4710n

c4710n

c4710n

c4710n

Maybe it’s time to summarize.


In short, keyword lists

  • have benefits on Erlang compatibility
  • have extra features, which are necessary for some cases:
    • ordering
    • allowing duplicate keys
  • have nice syntax sugar

As @michalmuskala said, when Elixir was created, Erlang didn’t have maps:

  • Elixir v0.5.0 (the first noticeable version) is released at 2012/03/25, and it started to use keyword lists, such as here (I picked this code randomly).
  • Erlang maps is introduced util OTP 17.0, in 2014.

For this historical reason, Elixir uses keyword lists for backward compatibility with existing Erlang codebase.

And, comparing to maps, keyword lists has two other main features:

  • ordering
  • allowing duplicate keys

For some cases, these two features are necessary or reasonable:

  • implement Elixir features as José said , such as:
    • try/catch/after
    • import Keyword, only: [get: 2, get: 3]

Because Elixir itself extensively uses keyword lists, many community projects also extensively use keyword lists, such as:

  • Ecto
  • Phoenix

And, as developers, we learned from existing mature code, so we wrote the same code, too. :wink:

Now, we already have maps. However, due to habit and existing codebases, we still tend to prefer using keyword lists.

Also, Elixir has nice syntax sugar for keyword lists, which allows us to use a keyword list without square braces as the last parameter. For example:

# huh...
CMS.create_post(attrs, %{by: charlie})

# better, IMO
CMS.create_post(attrs, by: charlie)

What about Erlang’s trends?

There is a trend in Erlang to use maps as options, which I don’t want to see in Elixir.


Read more

zacky1972

zacky1972

Creator of Pelemay

Thank you so much!

Where Next?

Popular in Chat/Questions Top

davearonson
I am looking for smallish problems to solve, using Elixir concepts beyond the basic syntax and concepts of Elixir as a language, such as ...
New
Allyedge
So, I want to get an Elixir book, but don’t know which one to get. Both Programming Elixir 1.6 and Elixir in Action looks interesting, b...
New
RKC07
I’m new to elixir. I did some coding in python and C. I want to learn elixir for starting my career in web development. I need suggestion...
New
pietrofxq
I’ve bought the following books: Programming Elixir 1.6 Programming Phoenix 1.4 Programming Ecto Functional Web Development with Elixir...
New
ggwc82
Looking to get started with FP and Elixir coming mainly from an OOP Rails and PHP background. My first question is, whats the best cours...
New
zeroexcuses
Besides https://elixir-lang.org/getting-started/basic-types.html are there any other well recommended “elixir by example” style resources...
New
InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
Emily
Preface: I’m not sure if thise is the right place, because this is not direclty Elixir related… but I’ve always got some of the best advi...
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New
Fl4m3Ph03n1x
Background I am trying to do the typical Ceaser cypher exercise in Elixir. The description of the exercise is as follows: Create an impl...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

We're in Beta

About us Mission Statement