Add support for keyword lists in Access.key/2 and Access.key!/1 - #15470
Conversation
|
I wasn't sure how defensive to be in the returned accessor function.
I could be more defensive and require the container is either I'm currently raising an I think it is important that regardless of container type, the raise is the same Thoughts? |
|
We are usually not that defensive when it comes to keywords. We may error but it is not a guarantee. Note the Thoughts on the |
ed7aecb to
61be27c
Compare
|
Overall looks good, but we might need to be slightly more defensive in the following case or bugs might slip: iex> put_in([1], [Access.key(:foo)], :bar)
[{:foo, :bar}, 1]The raw key fails with a non-optimal error but at least it fails: iex> put_in([1], [:foo], :bar)
** (FunctionClauseError) no function clause matching in Keyword.get_and_update/4 |
3182c0a to
4e9a98f
Compare
|
Is something like this what you had in mind @sabiwara ? I've added a guard in defguardp is_probably_keyword(list)
when is_list(list) and (list == [] or is_atom(elem(hd(list), 0)))It's clearer as a I figure that we just want to catch most mistakes, and not do a full I'm now also testing for this. |
|
OK, seems that the reason for the iex> put_in([1], [Access.key(:key)], 2)
[{:key, 2}, 1]
iex> Keyword.put([1], :key, 1)
[{:key, 1}, 1]Ideally we'd fix Given this context, although I kinda like the shallow check approach of If we want to add a shallow check, maybe the best would be having it inside |
|
Correct, we don't guarantee we validate it is a full blown keyword. I am not sure I would do it later and I don't think we should do it now. |
Full-blown yes, but WDYT of adding the shallow (not full blown) check to |
|
I see. I think last time we added the checks it broke some code that should not be relying on it. Perhaps it is something for us to revisit once we have the type system, since the type system will at least help catch some of it statically? |
Sounds good 👍 |
30949d4 to
c51f764
Compare
|
I've included your suggestions and rebased everything into a single commit. CI failed for |
|
💚 💙 💜 💛 ❤️ |
This adds Keyword list support to
Access.key/2andAccess.key!/1.Original discussion in #15468
I'd like to move some of. the doctest into
access_test.exsinstead.Access.keywas only tested in doctests, but the other accessors have dedicateddescribeinaccess_test.exs,but this is good enough for a discussion around the feature. :)