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


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.

1 Like

Thank you so much!

1 Like

There’s also probably the fact that when elixir was created erlang didn’t have maps.

1 Like

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