[swift-evolution] Optional Setting

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


I agree that would be nice. Just pointing out that it's nontrivial. If you
implement this custom operator today, you get different behavior.

Jacob

On Tue, Dec 15, 2015 at 3:21 PM, James Campbell <james at supmenow.com> wrote:

> If it has a value already the nit wouldn't call anything as it technically
> hasn't been set. Only if it already has a value does it try and set
> something in which case the didSet is called :)
>
> On Tue, Dec 15, 2015 at 11:16 PM, Jacob Bandes-Storch via swift-evolution
> <swift-evolution at swift.org> wrote:
>
>> 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
>>>
>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>
>
> --
>  Wizard
> james at supmenow.com
> +44 7523 279 698
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/7f004960/attachment.html>


More information about the swift-evolution mailing list