Bug 13923 – Specify guarantees on associative array iteration order volatility
Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-01T13:39:35Z
Last change time
2019-12-21T13:12:35Z
Assigned to
No Owner
Creator
Peter Alexander
Comments
Comment #0 by peter.alexander.au — 2015-01-01T13:39:35Z
Suppose I have an associative array:
string[int] a = [1:"a", 2:"b", 3:"c"];
Now, associative arrays have unspecified iteration order, but suppose I iterate the array and it iterates in order {2, 1, 3}.
What guarantees are provided on subsequent iterations? Specifically:
* If I iterate the array again, without modifying any keys or values, or inserting, removing, or rehashing; is the iteration order guaranteed to be the same?
* If I iterate the array again, with only modifying values (not keys), is the iteration order guaranteed to be the same?
The spec should specify what operations may alter the iteration order, and what operations will leave it intact.
Comment #1 by bugzilla — 2019-12-21T13:12:35Z
Answer to your questiones: You have no guarantees.
"In particular, in a foreach loop the order in which the elements are iterated is typically unspecified."
from https://dlang.org/spec/hash-map.html