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

Proposal: Dict constructors from tuples #4871

Closed
kmsquire opened this issue Nov 20, 2013 · 9 comments
Closed

Proposal: Dict constructors from tuples #4871

kmsquire opened this issue Nov 20, 2013 · 9 comments
Labels
kind:breaking This change will break code
Milestone

Comments

@kmsquire
Copy link
Member

Just putting out feelers: I'd like to propose:

  • removing constructing Dicts from tuples as is currently done

    Dict{K,V}(ks::(K...), vs::(V...)) = Dict{K ,V }(ks, vs)
    Dict{K }(ks::(K...), vs::Tuple ) = Dict{K ,Any}(ks, vs)
    Dict{V }(ks::Tuple , vs::(V...)) = Dict{Any,V }(ks, vs)
    

    (from https://github.com/JuliaLang/julia/blob/master/base/dict.jl#L302)

  • add a dict constructor from a list of tuple pairs (as the inverse of collect on a Dict)

Item 1 seems like a vestige from before we had literal dict construction--is this used anywhere?

Item 2 seems like an oversight.

@kmsquire
Copy link
Member Author

Of course, the easiest way to implement the second constructor would be

Dict{K,V}(kv::Array{(K,V)}) = Dict{K,V}(zip(kv...)...)

@StefanKarpinski
Copy link
Sponsor Member

I'm curious why you closed this? I do think that the collection constructors could use some review for consistency and completeness – Dicts, Sets, Arrays (which are already quite good).

@kmsquire
Copy link
Member Author

Sorry, should have explained better. After making the comment above, I simply created a pull request for the new Dict constructor here: #4872.

Since it uses the other constructors, there didn't seem much point in asking to remove them, and the constructor itself is trivial.

@JeffBezanson
Copy link
Sponsor Member

Dict literals work by calling the Dict(ks,vs) constructors :)

@kmsquire
Copy link
Member Author

How about changing the Dict constructor as you suggested in #4872, and making that the target of Dict literals?

@kmsquire
Copy link
Member Author

(Reopening because of this discussion...)

@kmsquire kmsquire reopened this Nov 21, 2013
@JeffBezanson
Copy link
Sponsor Member

That would be ok, but the current implementation was picked for efficiency. Of course I could use a hidden dict_literal() for this if we want to keep Dict simpler.

@kmsquire
Copy link
Member Author

I guess my main motivation in wanting to simplify the Dict constructors is to work around #4859, so that

Dict{A,B}(::Type{A},::Type{B})

can exist. Right now, it conflicts with the Dict tuple constructors. If #4859 were addressed, this wouldn't be as necessary.

The above construction would be useful because:

  1. This (or Feature Request: allow default values for unspecified type parameters #4859) would make other Dict types with more type parameters (OrderedDicts, DefaultDicts) less annoying, since they could have a similar set of constructors, and reasonable type defaults could be provided
  2. It's more consistent with Array constructors

@kmsquire
Copy link
Member Author

I wonder how much slower would this be:

Dict(ks, vs) = Dict{eltype(ks), eltype(vs)}(ks,vs)

This would subsume the current interfaces, and still allow

Dict{A,B}(::Type{A},::Type{B})

to exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:breaking This change will break code
Projects
None yet
Development

No branches or pull requests

3 participants