fireproofsocks

fireproofsocks

Why keyword lists for opts instead of maps?

I’m sure this has been asked elsewhere (but forgive me I could not find something that addressed this specifically)…

Why do Elixir functions usually take keyword lists as options? Why not maps? Keyword lists are problematic especially if you’d like to pattern match on a map key to route to different functions, for example. Is there a historical or performance-based reason why keywords are used over maps?

Most Liked

josevalim

josevalim

Creator of Elixir

Elixir had an option to choose either keyword lists and maps for options. The reason why we chose keyword lists are because they preserve user ordering and they allow duplicate keys. We use both features in Elixir itself.

For example, in try/catch/after, we warn if you put after before catch, as that would read weird. Using maps would not allow us to warn in those cases, as you always have alphabetical ordering.

When you do import Keyword, only: [get: 2, get: 3], those are duplicate keys. Keyword.__info__(:functions) returns duplicate keys too.

In other words, there are scenarios where keyword lists can be useful and unifying opts under keyword lists is reasonable. Community projects, such as Ecto, leverages them too. Plus other benefits such as Erlang compat.

gregvaughn

gregvaughn

Keyword lists are very core to the Elixir syntax itself. For example

def the_answer do
  42
end

is syntax sugar for

def the_answer, do: 42

or even

def(the_answer, [do: 42])

which is basically 2 parameters passed to the def macro. The last one is a keyword list with a :do key in it.

This is why all your functions can take a keyword list without square braces as the last parameter – because the core language syntax uses that.

18
Post #9
christhekeele

christhekeele

This is worth highlighting, it gives you a CLI-repeated-flag-like interface for options.

10
Post #4
hauleth

hauleth

Elixir keyword lists are strict subset of proplists.

This could be done for maps as well TBH.


My reasons:

  • Maps are way younger than property lists/keyword lists
  • Keyword lists allow for duplicated keys which can be useful sometimes
LostKobrakai

LostKobrakai

Maybe not an exhaustive list, but some reasons:

  • Erlang historically uses proplists. Not 100% the same but similar.
  • The keyword syntax does have some nice sugar
  • Elixir used to have HashDict instead of maps, so keyword lists might have been supported before maps were

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement