-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Keyword.keys/1 accepts non-keyword lists and returns their keys #10010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In general the typespecs and documentation represent the indented use. The function can happen to work on other things as well due to implementation details, but this is not guaranteed and shouldn't be relied upon. Specifically some functions in |
eksperimental
added a commit
to eksperimental-forks/elixir
that referenced
this issue
Jul 20, 2020
Previously it would return something like this: ** (FunctionClauseError) no function clause matching in anonymous fn/1 in Keyword.keys/1 The following arguments were given to anonymous fn/1 in Keyword.keys/1: # 1 {"fetch/2", {:fetch, 2}} Closes elixir-lang#10010, and it improves 3fd68ef
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Environment
Current behavior
as you can see in the above snippet when using
Keyword.has_key?/2
it properly results in an error saying it failed to match any clause because the key must be anatom
(as shown inis_atom(key)
). However, in the following functionKeyword.keys/1
it returns the"my_key"
key which is not anatom
.This behaviour feels inconsistent to me. Because, AFAIK,
Keyword.t
represents a list of pairs{atom(), term()}
, as seen here:Going further down this path I've noticed that the
lists.keymember/3
function from Erlang's core library also provides inconsistent behaviour:EDIT: this snippet above is actually correct because in Erlang's
lists
the key can be anything as per the docs.Finally, looking at Elixir's implementation of
Keyword.keys/2
here this seems to result due to using Erlang'slists
module which probably is the root cause of these behaviours.Expected behavior
The
Keyword
module only accepts actualKeyword.t()
.Solution
I'm not sure what the solution here would be since Elixir is a dynamic language and we don't know if a given list respects the
Keyword.t
spec before actually evaluating it. Maybe, check if each key is anatom
and raise an error otherwise? Or just return the keys which areatom
?The text was updated successfully, but these errors were encountered: