-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: Define API invariance for keys, values, getindex and pairs #36175
base: master
Are you sure you want to change the base?
Conversation
A case study:
|
While clarification is always good(!) I think that the current understanding of Generators expects them to only depend on their input arguments (and no hidden state) since, strictly speaking, there are no guarantees/claims about the order in which it'll be iterated. Generator(f, iter)
Given a function f and an iterator iter, construct an iterator that yields the values of f applied to the elements
of iter. |
Thanks, that's a good point. Strictly speaking, I don't think |
To clarify, in the example you gave most recently, I think having Edit: We also might consider to change the Edit2: in the case of collections and iterators (not dicts) |
|
||
```julia | ||
isequal(map(first, paris(collection)), keys(collection)) | ||
isequal(map(last, paris(collection)), values(collection)) |
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.
Note, that currently neither Array
nor Dict
satisfies these relationships, because map
is not defined for the return types of pairs
:
julia> coll = [10, 20];
julia> map(first, pairs(coll))
ERROR: map is not defined on dictionaries
IMO the claim, that the order of the generated iterator follows the order of the base iterator is so natural, that it is expected by most users. In my understanding it is implied in the description. That would be consistent with the documentation of
|
the following relationships: | ||
|
||
```julia | ||
isequal(map(first, paris(collection)), keys(collection)) |
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.
🇫🇷
There are discussions for extending
keys
#34678 andpairs
#34851 API. Rather than extending the implementation purely for convenience, I suggest to document the expected relationships between the functions. This clarifies what to expect from these APIs without looking at each implementation. Otherwise, it is difficult to use these functions in generic code.Concretely, for
keys
,values
, andgetindex
I suggest:For
pairs
:Since it is a breaking change to impose more strict API requirements for already existing and used API, I only used a very loose phrasing "It is highly recommended." It is up to discussion if upgrading this to "must" is a
minor change
.close #34851