Qqwy
Why is Keyword restricted to atom keys?
I am working on an implementation of a forth-like language on top of Elixir.
One of the things that is a major part of any forth-like language, is the dictionary of defined functions (called words). This is usually implemented as a linked list, so that multiple vocabularies (i.e. execution contexts) might coexist without conflicting definitions (they are re-using the same tail of the list, but each context might have its own head).
I wanted to build this in Elixir using a list. But now I found out that functions like Keyword.get, Keyword.put, etc. only work if the key is an atom. Why is this restriction on atom keys there?
The foo: 1, bar: 2 syntactic sugar of course desugars to a ‘keyword list’, but even without using this syntactic sugar it would be useful to use Keyword.get, Keyword.get_values etc. with different types of keys.
Most Liked
NobbZ
You should be able to use :lists.key*/* (erlang documentation) for what you want. Also it should be very simple to write up functions as needed for yourself.
I do think, that the overall fact has to do something with the expenses of comparing anything else than atoms for equality, but this is in fact only a guess.
gregvaughn
There used to be a data structure called a ListDict, which fundamentally was something like [{"foo", 1}, {"bar", 2}] that could use binaries for the keys, but didn’t require them to be IIRC. It was a confusing time between the Dict module and maps being introduced, and when APIs were refactored we lost the convenience of that ListDict structure. I too have missed it.
Popular in Questions
Other popular topics
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








