[swift-evolution] Optional Setting

Jacob Bandes-Storch jtbandes at gmail.com
Tue Dec 15 17:16:30 CST 2015


One possible caveat is with custom setters.

If "a" already has a value, does "a ??= b" call the custom
setter/willSet/didSet, or does it see the nil and short-circuit?

This can be implemented today:

    func ??=(inout lhs: T?, @autoclosure rhs: () -> T?) { if lhs == nil {
lhs = rhs() } }

However, the use of "inout" will always cause the didSets to be triggered
at the call site, when just using if-statements instead wouldn't have done
so.

Jacob

On Tue, Dec 15, 2015 at 3:10 PM, Brent Royal-Gordon via swift-evolution <
swift-evolution at swift.org> wrote:

> > I think that the existing syntax for “??” handles this need fairly well
> without requiring an additional assignment operator:
> >
> >       a = a ?? []
>
> When the variable is `a`, sure. When it’s
> `scoreboardViewController.selectedScoreboard`, not so much.
>
> +1 from me, though I prefer the `??=` spelling to match the `??` operator
> more closely.
>
> --
> Brent Royal-Gordon
> Architechies
>
> _______________________________________________
> 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/20151215/4e858fc6/attachment.html>


More information about the swift-evolution mailing list