-
-
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
dict docs: document that ordering of keys/values/pairs match iterate #56842
Conversation
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.
In my interpretation, this guarantees that keys
, values
, and pairs
iterate in the same order for all collections (not just Dict
s).
I think this is a reasonable guarantee for this API.
I don't think the CI failure is related to this PR ? |
test/dict.jl
Outdated
@test [k for (k,v) = d] == | ||
[k for k = keys(d)] == | ||
[k for (k,v) = pairs(d)] == | ||
collect(keys(d)) |
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.
Do these need to be collect
ed or is a check with any
/all
(consuming the iterators in the process) also good here? Would save on some allocations.
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.
a check with any/all
How exactly?
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.
I don't think we should worry about optimizing allocations here; this code is run once with n=100 so it will be compile-time dominated anyway.
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.
How exactly?
Something like this:
@test all(zip(d, keys(d), pairs(d))) do (kv, k, kpair)
first(kv) == k == first(kpair)
end
(might need some adjustment in the argument destructuring of the do
block - not 100% sure this unpacks correctly)
I don't think we should worry about optimizing allocations here
I'm not worried about allocations, that would just be a sideeffect (of course, consuming fewer resources in CI is always nice to have). In my opinion, a all
+zip
version is just a bit cleaner and makes sure to only rely on iteration, and not incidental behavior of collect
or other unrelated stuff :)
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.
@Seelengrab That's a nice suggestion. I updated the tests.
test/dict.jl
Outdated
foreach(zip(dict, keys(dict), values(dict), pairs(dict))) do (d, k, v, p) | ||
@test d == p | ||
@test first(d) == first(p) == k | ||
@test last(d) == last(p) == v | ||
end |
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.
In case there's a bug that decouples keys
/values
/iterate
/pairs
, this will cause up to 300 individual failures, each showing up in the log. I think it'd be better for debugging to only have one failure through @test all(...)
, instead of three @test
per key-value-pair in the dict.
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.
Better now?
Fix #56841.
Currently the documentation states that keys(dict) and values(dict) iterate in the same order. But it is not stated whether this is the same order as that used by pairs(dict), or when looping, for (k,v) in dict.
This PR makes this guarantee explicit.