Qqwy

Qqwy

TypeCheck Core Team

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

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

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.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement