[swift-evolution] [Review] SE-0024 "Optional Value Setter `??=`"

Joe Groff jgroff at apple.com
Tue Feb 16 12:26:16 CST 2016

I'm not sure this is the best design for Swift. In other languages where I've used similar features, particularly Perl and Ruby, the "set a dictionary key to a default value" was by far the most common use. Particularly in Ruby, with its reference-semantics containers, it's useful to pass down 'foo[x] ||= []' as an expression into a function or method that would then populate the array:

def populate(array)
  array << [1,2,3]

hash = {}

populate(hash["foo"] ||= [])

Though ugly, that's a handy bit of expressivity you don't get with the proposed '??=' design. Even if '??=' returned the result, it wouldn't be able to return it as an lvalue.

I think a more expressive alternative approach to this problem would be to extend Dictionary with a `subscript(_:orDefault:)` member:

extension Dictionary {
  subscript(key: Key, orDefault value: Value) -> Value {
    get {
      return self[key] ?? value
    set {
      self[key] = newValue

For the common use case of providing a default value for a dictionary key, this has several advantages. It can be used as an lvalue, as in `populate(&hash["foo", orDefault: []])`. It also avoids storing to the dictionary if the defaulted value is only read. (I think in the fullness of time, the default value probably belongs as a type parameter, e.g. `DefaultDictionary<Key: String, Value: [String], Default: []>`, but we're a ways away from that today.) It's true that this feature is dictionary-centric, whereas `??=` applies generally to any optional, but as others have noted, I'm not sure that generality is a *feature* we want to encourage.


> On Feb 12, 2016, at 9:15 PM, Douglas Gregor via swift-evolution <swift-evolution at swift.org> wrote:
> Hello Swift community,
> The review of SE-0024 "Optional Value Setter `??=`" begins now and runs through February 18, 2016. The proposal is available here:
> https://github.com/apple/swift-evolution/blob/master/proposals/0024-optional-value-setter.md <https://github.com/apple/swift-evolution/blob/master/proposals/0024-optional-value-setter.md>
> Reviews are an important part of the Swift evolution process. All reviews should be sent to the swift-evolution mailing list at
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> or, if you would like to keep your feedback private, directly to the review manager. When replying, please try to keep the proposal link at the top of the message:
> Proposal link:
> https://github.com/apple/swift-evolution/blob/master/proposals/0024-optional-value-setter.md <https://github.com/apple/swift-evolution/blob/master/proposals/0024-optional-value-setter.md>
> Reply text
> Other replies
>  <https://github.com/apple/swift-evolution#what-goes-into-a-review-1>What goes into a review?
> The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:
> What is your evaluation of the proposal?
> Is the problem being addressed significant enough to warrant a change to Swift?
> Does this proposal fit well with the feel and direction of Swift?
> If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
> How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
> More information about the Swift evolution process is available at
> https://github.com/apple/swift-evolution/blob/master/process.md <https://github.com/apple/swift-evolution/blob/master/process.md>
> Thank you,
> Doug Gregor
> Review Manager
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160216/e5225429/attachment.html>

More information about the swift-evolution mailing list