-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Indexable #3087
Add Indexable #3087
Conversation
Big 👍, I'm just not sure about the name, it makes me think of memory, probably because RAM stands for random access memory. What about my initial idea of |
Ah, sounds good. Then it's also maybe more clear that you can index into it, but not necessarily mutate it. |
8839579
to
63b65cd
Compare
I don't think the documentation issue you mention is a stopper. At most, is something might push the docs to handle better the inherited methods due to modules. |
Should |
@@ -0,0 +1,469 @@ | |||
# A container that allows accessing elements via a numeric index. |
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.
It seems fair to asume index are 0..size-1 and that all indices should return a value without raising.
It should be in the docs for clarification I think
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.
Good idea. I added the docs for this in the module, and in the unsafe_at
method
78f7777
to
6df4b09
Compare
@RX14 In the next release I'll change |
6df4b09
to
af42af8
Compare
I can't see why |
@ozra Yes, definitely. The |
This was proposed many times in the past and I think it's time to implement this.
All started with me trying to implement the sieve exercise on exercism. I came up with a solution very similar to that of @jhass ( http://exercism.io/submissions/dd85bc5d9c6947dd98fcc807db62afe8 ), using Array, and then wanted to use BitArray only to find out it didn't have an
each
method. So I thought about adding it, but it was very similar to that of Array. And that of Slice, and Deque. So I extracted all those iterators into a separate class, but then thought many other methods were similar. Then I remembered about the idea of adding anIndexable
module so all implementations can be in sync, and also to avoid a lot of boilerplate in every implementation.Indexable needs a protected
unsafe_at(index)
to be implemented. This allows for some methods to be implemented much more efficiently (I did some benchmarks), to avoid some index bounds check when it's safe to avoid them.I found a couple of compiler bugs: if you include a generic module that includes another generic module, that second generic module isn't seen in restrictions. Then protected methods aren't correctly considered in generic modules so for now I made the
unsafe_at
methods public. But we can change these when we fix the compiler bugs.One drawback of this change is that now some Array methods are not immediately seen in the Array docs, but you need to jump to
Indexable
, so newcomers might be confused with this.Indexable only provides read access, not write access. I didn't find the write methods in Array, Slice, StaticArray and Deque to be similar, and there's also the thing that Tuple is read-only.