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?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance











First 10 of 13 Posts
LostKobrakai
Maybe not an exhaustive list, but some reasons:
hauleth
Elixir keyword lists are strict subset of proplists.
This could be done for maps as well TBH.
My reasons:
christhekeele
This is worth highlighting, it gives you a CLI-repeated-flag-like interface for options.
dimitarvp
Joining the last two. I’d prefer maps any day for config but prioritising the last given config key is impossible with maps.
I do however do my best to only use maps for config in my internal projects.
Phillipp
I find using keyword lists with
Keyword.get/3very handy because you can easily define a default value. Might be somehow possible with Maps too, I guess.NobbZ
Map.get/3…Though as others already have said, the big plus of KW is that it enables for having a key multiple times, and they are also ordered…
Try
ectowith maps instead of KW… You won’t have much joy…lucaong
Yep, I think it’s a mix of historical and practical reasons (the latter including possibility of duplicated keys, and explicit ordering).
You are right that keyword lists are more inconvenient when using pattern matching. But then again, I think they make a good outside interface, and usually I validate and parse options into another data structure (like a map or struct) as soon as they get within the boundary of my module or library.
gregvaughn
Keyword lists are very core to the Elixir syntax itself. For example
is syntax sugar for
or even
which is basically 2 parameters passed to the
defmacro. The last one is a keyword list with a:dokey 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.
al2o3cr
Maps weren’t introduced until OTP 17.0 in 2014, and were experimental until at least 2015.
DaAnalyst
Personally, I like KW lists because of a) order, b) syntactic sugar.
is so much nicer than:
When I need accessing the options in a map-like fashion, I’m willing to pay the price of having the KW list converted to a map in my function.
Besides, maybe what you sometimes need is precisely a list-like pattern matching e.g.: