[swift-evolution] Dictionary Enhancements
Ole Begemann
ole at oleb.net
Sun Feb 19 13:22:48 CST 2017
> On 17 Feb 2017, at 01:26, Ben Cohen via swift-evolution <swift-evolution at swift.org> wrote:
>
> Here is a list of commonly requested changes/enhancements to Dictionary, all of which would probably be appropriate to put together into a single evolution proposal:
>
> init from/merge in a Sequence of Key/Value pairs (already raised as SE-100: https://github.com/apple/swift-evolution/blob/master/proposals/0100-add-sequence-based-init-and-merge-to-dictionary.md <https://github.com/apple/swift-evolution/blob/master/proposals/0100-add-sequence-based-init-and-merge-to-dictionary.md>).
> make the Values view collection a MutableCollection (as in this PR: https://github.com/apple/swift-evolution/pull/555 <https://github.com/apple/swift-evolution/pull/555>).
> Add a defaulting subscript get (e.g. counts[key, default: 0] += 1 or grouped(key, default:[]].append(value)).
> Add a group by-like init to create a Dictionary<K,[V]> from a sequence of V and a closure (V)->K.
Out of interest, how would you implement this? Does it require a generics feature that's slated for Swift 4? I tried two approaches that don't compile in a current Swift 3.1 snapshot (and I'm getting a segfault with both examples in a dev snapshot from 2017-02-14):
1)
extension Dictionary {
// error: same-type constraint 'Value' == '[S.Iterator.Element]' is recursive
init<S: Sequence>(values: S, groupedBy: (S.Iterator.Element) -> Key)
where Value == [S.Iterator.Element] {
...
}
}
}
2)
// error: reference to generic type 'Array' requires arguments in <...>
extension Dictionary where Value == Array {
init<S: Sequence>(values: S, groupedBy: (S.Iterator.Element) -> Key)
where S.Iterator.Element == Value.Element {
...
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170219/6cc3b99e/attachment.html>
More information about the swift-evolution
mailing list