Repository navigation
RFC: Change MultiDict constructors to more closely match the abstraction: accept _multiple pairs_ - #656
RFC: Change MultiDict constructors to more closely match the abstraction: accept _multiple pairs_#656NHDaly wants to merge 8 commits into
Conversation
Before, constructors would accept _either_ (k=>v, k=>v...), or (k=>[v],
k=>[v]...). Each worked in some contexts, but neither in all contexts.
Also, before, generators did not work.
Now, there are two simple rules:
1. MultiDict constructors expect pairs or tuples or generators of {K,V}:
- `MultiDict(1=>1,1=>2)`, `MultiDict{Int,Int}(1=>2,2=>3)`, etc
2. To manually construct a MultiDict with {K,Vector{V}), you can
construct with the _underlying Dict_ directly.
- `MultiDict(Dict(1=>[1,2])`, `MultiDict{Int,Int}(1=>[2],2=>[3])`, etc
- Fixes broken constructor by adding `grow_to!` overload. We need this
because MultiDict isn't an AbstractDict.
- Adds `empty(::MultiDict, ::Type{K}, ::Type{V})`, which was needed in
the process, and also makes us more standard! :raised-hands:
|
As an example, I left this comment here because i thought that the generator constructor was just broken: |
There was a problem hiding this comment.
I've reviewed the PR, and requested some stylistic changes. What you're proposing is a breaking change, so I would like to hear @oxinabox 's opinion about this.
Apply @eulerkochy's formatting suggestions Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
|
yes, we do want to batch up the breaking changes into a single release. |
|
😭 oh no! Bummer. I'm fine leaving all of these on like one big "multi-dict" branch or something that we can wait to merge until you're comfortable with another breaking change? Does that work? I guess, the kind of nice thing about this package is that probably most people are using it for only a few container types, so hopefully each breaking API change only affects some (hopefully different) people? |
|
It we can't workout a way to deprecate them safely then yes, putting it onto a branch would be best. |
|
Okay, makes sense! If you can create a new branch (maybe |
|
Or I guess you're saying maybe we can still support the older constructors but deprecated? Unfortunately I don't think so, without the behavior remaining ambiguous and confusing. For example, the fact that whether or not you add the type parameters changes the meaning of the pair arguments seems completely broken to me: julia> MultiDict(1=>[2,3]) # Treated as a single pair
MultiDict{Int64,Array{Int64,1}}(Dict(1 => [[2, 3]]))
julia> MultiDict{Int,Int}(1=>[2,3]) # Treated as a normalized representation of all pairs
MultiDict{Int64,Int64}(Dict(1 => [2, 3]))I think that in order to make the constructors behave consistently, we'll need to break the old behavior. I'm very open to suggestions for how to do this change in a safe, backwards compatible, deprecation way though if you see such a potential path!! :) Thanks again for your time :) |
Fixes constructors that must do type-promotion, e.g.:
```julia
julia> MultiDict(1 => 2, 1 => "hello")
MultiDict{Int64,Any}(Dict{Int64,Array{Any,1}}(1 => [2, "hello"]))
```
7799147 to
74fcbc5
Compare
|
Thanks friends! Sorry it took me so long to get to this. Now that i have the committer bit on this repo, i've created a new MultiDict-reboot branch, and adjusted this PR to point to it. i'm going to do that with all the MultiDict PRs that have breaking changes, so we can do them all together. I still think it's nice to review and merge this as individual PRs though, to make reviewing easier. :) Anyone wanna do a final review and/or approval? ❤️ |
From my understanding, the MultiDict is like a Dict, except that it can hold multiple pairs that have the same key. Much like bag-semantics in SQL.
And the current constructors do imply this as well:
But before this PR, the constructors actually sometimes accept pairs of {K,V}, in some contexts, but they also sometimes accept pairs of {K,Vector{V}}, in other contexts. But neither would work in all contexts.
Here are some examples of the failures before this PR:
New Proposal
I tried fixing just some of those weird constructors, but the inconsistencies were too hard to grapple with. I think the new rules I'm proposing are quite logical, concise, easy to understand, and keep close to the core concept. But they will be a breaking change in the API. Is that okay?
Here's what I'm proposing and what this PR implements:
MultiDict(1=>1,1=>2),MultiDict{Int,Int}(1=>2,2=>3),MultiDict{Int,Int}([(1,1), (1,2)]), etcMultiDict(Dict(1=>[1,2]),MultiDict{Int,Int}(Dict(1=>[2],2=>[3])), etcThe tests used the constructors that passed all values in a vector in a few places, so I've changed those to either enumerate the pairs, or to use the inner Dict constructor.
Does this seem reasonable? I think it makes the type much easier to use, more consistent, and more flexible. As a user, it took me a while before finding a constructor that actually did what I wanted, and so i think this will be helpful to others! :)
If we do go with this, should I bump the minor release number? (0.18 => 0.19)
Let me know what you think! Thanks! :)