Skip to content
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

Do more work to prevent iterator invalidation for Dict's? #11316

Open
johnmyleswhite opened this issue May 17, 2015 · 3 comments
Open

Do more work to prevent iterator invalidation for Dict's? #11316

johnmyleswhite opened this issue May 17, 2015 · 3 comments
Labels
design Design of APIs or of the language itself kind:speculative Whether the change will be implemented is speculative

Comments

@johnmyleswhite
Copy link
Member

Should we try to do more to prevent iterator invalidation for Dict's (and possibly for other types as well)?

Contrast this Julia code:

  _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.4.0-dev+4878 (2015-05-17 18:08 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit b3116d4 (0 days old master)
|__/                   |  x86_64-apple-darwin14.3.0

julia> x = Dict("a" => 1, "b" => 2)
Dict{ASCIIString,Int64} with 2 entries:
  "b" => 2
  "a" => 1

julia> for (k, v) in x
           println((k, v))
           delete!(x, "a")
       end
("b",2)
ERROR: UndefRefError: access to undefined reference
 in next at dict.jl:721
 in anonymous at no file

with this Python code:

Python 2.7.9 |Anaconda 1.8.0 (x86_64)| (default, Dec 15 2014, 10:37:34) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> x = {"a": 1, "b": 2}
>>> 
>>> for k, v in x.iteritems():
...     print((k, v))
...     del x["b"]
... 
('a', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
>>> 

I would argue that Python's error is more helpful for debugging, although producing such an error requires making the iteration protocol do more run-time checking on every iteration to see whether the iterator has been invalidated.

Was this a conscious decision already made for the language? Skipping these kinds of checks does feel like the Julian way, but I wanted to confirm that this was intended behavior.

@JeffBezanson
Copy link
Sponsor Member

See also #9573 and #9595

@StefanKarpinski
Copy link
Sponsor Member

Also #10116, which improved this situation.

@StefanKarpinski StefanKarpinski added design Design of APIs or of the language itself kind:speculative Whether the change will be implemented is speculative labels Aug 26, 2016
@StefanKarpinski StefanKarpinski added this to the 0.6.0 milestone Aug 26, 2016
@StefanKarpinski
Copy link
Sponsor Member

Ordered dicts might help this since modifying a dict does not change the iteration order in arbitrary ways. Especially if reassigning an existing key leaves it in the same order in the dict. It would take some careful design, but it might be possible to make it safe to mutate a dict while iterating it.

@JeffBezanson JeffBezanson modified the milestones: 1.0, 0.6.0 Dec 29, 2016
@JeffBezanson JeffBezanson modified the milestones: 1.x, 1.0 Jun 4, 2017
@DilumAluthge DilumAluthge removed this from the 1.x milestone Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself kind:speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

4 participants