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

Should iteration over Associatives return Pairs #12261

Closed
Keno opened this issue Jul 22, 2015 · 9 comments · Fixed by #12327
Closed

Should iteration over Associatives return Pairs #12261

Keno opened this issue Jul 22, 2015 · 9 comments · Fixed by #12327
Labels
needs decision A decision on this change is needed

Comments

@Keno
Copy link
Member

Keno commented Jul 22, 2015

Right now we have

julia> first(Dict(1=>2))
(1,2)

I'm proposing this be changed to

julia> first(Dict(1=>2))
1 => 2

i.e. treating dicts as a list of key value pairs for the purposes of iteration. Making them a pair makes clear what the return value means. I think this is a fairly non-breaking change as Pair essentially behaves as a 2-element tuple anyway.

@Keno Keno changed the title Should iteration over Associative's return Pairs Should iteration over Associatives return Pairs Jul 22, 2015
@Keno Keno added the needs decision A decision on this change is needed label Jul 22, 2015
@StefanKarpinski
Copy link
Member

+10, I feel like this was just a possible oversight in the Pair change, or maybe there was some compatibility reason for it.

@stevengj
Copy link
Member

Seems reasonable to me; for (k,v) in dict and similar should still work, as @Keno says.

@lindahua
Copy link
Contributor

+1

@ivarne
Copy link
Member

ivarne commented Jul 23, 2015

+1

Ref #9905 for previous discussion and oversight.

@JeffBezanson
Copy link
Member

This is fine but there's an interesting point here regarding covariance. If the eltype of a Dict{Any,Any} is Tuple{Any,Any}, it can return more specific tuples like Tuple{Int,Int} for particular elements. With Pairs, every item iterated will have to be a Pair{Any,Any} exactly.

@JeffBezanson
Copy link
Member

To be a bit more concrete, returning k=>v as an element of a Dict might be wrong. One needs to be careful to return a Pair{K,V}(k,v).

@StefanKarpinski
Copy link
Member

I'm not sure what the practical consequences of that would be...

@JeffBezanson
Copy link
Member

Not many, but for example 1=>2 is === to an element of Dict(1=>2), but not to an element of Dict{Any,Any}(1=>2).

@ivarne
Copy link
Member

ivarne commented Jul 27, 2015

Ref #12319.

We should consider making this change universal for all functions that return key => value pairs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants