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.

2 Likes

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.

1 Like

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.

1 Like

It is just an Elixir convention, Erlang often broke that style.

Precisely this.

Pretty sure the Elixir List module also has those functions.