Proposal: Add a sequence-based initializer to Dictionary

In some dictionary implementations, key-value pairs are added with an add() method which disallows duplicate keys to be inserted with a runtime exception. Providing an additional method of set() allows for the ability to ignore duplicate keys so that it feels more like dict[key] = value. It might be interesting to provide this as a selectable behavior within an additional constructor's arguments.

Dictionary(allowDuplicate: true, pairs: [("z", 1), ("z", 2), ("z", 3), ("z", 4)]) so that the dictionary behavior remains in line with the compile time checks.

Gregg

Hi Gregg —

The Dictionary(_:uniquingKeysWith:) initializer was added for this purpose in Swift 4—please see https://developer.apple.com/documentation/swift/dictionary/2892961-init

Nate

···

On Jan 8, 2018, at 1:02 PM, Gregg Wonderly via swift-evolution <swift-evolution@swift.org> wrote:

In some dictionary implementations, key-value pairs are added with an add() method which disallows duplicate keys to be inserted with a runtime exception. Providing an additional method of set() allows for the ability to ignore duplicate keys so that it feels more like dict[key] = value. It might be interesting to provide this as a selectable behavior within an additional constructor's arguments.

Dictionary(allowDuplicate: true, pairs: [("z", 1), ("z", 2), ("z", 3), ("z", 4)]) so that the dictionary behavior remains in line with the compile time checks.

Gregg

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Okay, that does work for my example. But why wouldn’t the key also be available so that you might be able to have selective handling of first vs last for each key?

Gregg

···

On Jan 8, 2018, at 4:03 PM, Nate Cook <natecook@apple.com> wrote:

Hi Gregg —

The Dictionary(_:uniquingKeysWith:) initializer was added for this purpose in Swift 4—please see https://developer.apple.com/documentation/swift/dictionary/2892961-init

Nate

On Jan 8, 2018, at 1:02 PM, Gregg Wonderly via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

In some dictionary implementations, key-value pairs are added with an add() method which disallows duplicate keys to be inserted with a runtime exception. Providing an additional method of set() allows for the ability to ignore duplicate keys so that it feels more like dict[key] = value. It might be interesting to provide this as a selectable behavior within an additional constructor's arguments.

Dictionary(allowDuplicate: true, pairs: [("z", 1), ("z", 2), ("z", 3), ("z", 4)]) so that the dictionary behavior remains in line with the compile time checks.

Gregg

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

Having only the values allows you to do some nice higher-order things, like:

  let histogram = Dictionary(values.map { ($0, 1) }, uniquingValuesWith: +)

In other words, the same functions you could use with `reduce` can be used with `init(_:uniquingValuesWith:)`. If the function took a third parameter, we would lose this property.

There's a bunch of very reasonable things you can't do with `Dictionary(_:uniquingValuesWith:)`; for instance, you can't cause the initializer to fail by returning `nil`, only by throwing an error. Supporting these features would either complicate cases where you don't need them, or require us to provide another overload. Given that `Dictionary(_:uniquingValuesWith:)` and friends are just conveniences, and reimplementing them requires only a few lines of straightforward code, it's just not worth supporting every plausible variant.

(For what it's worth, the Swift Evolution proposal which added this initializer, [SE-0165 Dictionary & Set Enhancements](https://github.com/apple/swift-evolution/blob/master/proposals/0165-dict.md\), touches on some of these issues. A big part of a proposal's role is to document the reasons for Swift's designs; if you ever wonder why something in Swift is designed the way it is, the feature's evolution proposal is a good place to start.)

···

On Jan 8, 2018, at 3:23 PM, Gregg Wonderly via swift-evolution <swift-evolution@swift.org> wrote:

But why wouldn’t the key also be available so that you might be able to have selective handling of first vs last for each key?

--
Brent Royal-Gordon
Architechies