-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Access protocol for all lists of tuples #611
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
Conversation
@@ -31,8 +31,13 @@ defimpl Access, for: List do | |||
|
|||
""" | |||
|
|||
def access(list, atom) when is_atom(atom) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it make sense to keep a special atom clause for Keyword here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? It's just an extra function call.
Let's try to keep access as fast as possible.
I have many mixed feelings right now. :) Historically, the access protocol worked just like you showed but we have changed it with other things which makes it worthy to re-evaluate the whole situation. Here are some points of concerns:
In other words, everything you pass to the access protocol you should be able to pass to TL;DR I like the idea but there are many loose ends we need to discuss before merging it. Feedback is very welcome. |
@josevalim I'm glad you like the idea because it really cleans up a lot of my code and it seemed intuitive to me. I disagree with point 1, agree tentatively with point 3. I think the access protocol should be allowed on any structure where it makes sense. To me that includes tuple lists, dicts, keywords, records -- anything that has clear keys. |
@josevalim What do we need to discuss? |
This is indeed a possible interpretation of the access protocol. In general, a protocol is a way to implement polymorphism, so ultimately you don't care to watch you are dispatching to. In this sense, it doesn't make much sense to group a record and a dict. You rarely want to mix them. Most of all, as soon you need something else (let's say, update a field), you need to rely on specific dict and records semantics, so the access protocol breaks down. On the other hand, since the access protocol is also a syntax wise feature, we feel compelled to change its main goal to simply allowing a key based access on different structures and that is it (which was its original definition). If we go into this direction, it means that usually when you see a Not supporting this approach means the code becomes (a bit) verbose, like your example above, that you need to at least convert your stars list to an In the second approach, it would also mean that it would be very hard to extend the access protocol, because the structures supported in it are too different from each other. Let's suppose we want to have an equivalent to putting a key in the access protocol, so we don't need to go through So that's what the point 1) above boils down to. Honestly, I didn't like at all (in previous versions) the fact I couldn't reason about what |
@josevalim I think my implementation of it for lists is just fine. I don't think it should be converted to a |
@devinus just for curiosity, assuming that you convert those examples to a Dict, what would be missing? You wouldn't convert it to a dict on each access, but when you create it. It makes sense, because you are using them as a dict anyway. No? |
Also, you can't just turn it into a Dict without doing it recursively. Example: foo = HashDict.new [{"a", [{"b", 1}]}]
foo["a"]["b"] #=> FunctionClauseError |
@josevalim Can we get movement on this? For the record I'm still of the opinion that this is a good change. 😸 |
How about we write down all the pros and cons of this change and take it from there? |
I'll keep an updated list of my thoughts here: Pros
|
@devinus can you also think of cons as well? |
@yrashk I don't see cons the same way @josevalim does. I think this makes the whole protocol simpler. |
I am agreeing with merging this feature. However, there is one requirement left before we merge this feature: we need to have a set of Dict functions that works on lists. The access protocol is meant to work on Dicts and by merging this feature and extending the Dict module to work with lists, we will:
The Dict functions that works on a list could be implemented inside the Dict module:
The implementation should be easy because everything is already implemented in the Keywords module. We just need to copy them over and remove the This brings us to the second point: what about Keyword? I am aware of the duplication of code we will have but that is meant to be temporary until we have some decision regarding frames. Keyword will be kept as atom only keys (for now) because it gives us enough space for maneuver in the all situations:
With this in mind, I believe we can proceed with this. We just need the implementation for lists handling in the Dict module. So who is going to contribute it? :) |
This is incredibly useful for dealing with things like JSON.
For example, in my app I assumed this would work:
Other possibilities: