I ended up creating a ??= operator about a month ago to do something very 
similar.  It’s a shame it won’t be made part of the official library.  
Although, it is very easy to add.


Just like the ?? operator the default value is only evaluated if the optional 
in nil.  However, unlike ?? it will

change the optional to be equal to the value on the right, iff the optional was 
nil.


infix operator ??= {

    associativity right

    precedence 90

    assignment

}


public func ??=<VALUE>(inout optional: VALUE?, @autoclosure defaultValue: () 
throws -> VALUE) rethrows -> VALUE {

    if let value = optional {

        return value

    } else {

        let value = try defaultValue()

        optional = value

        return value

    }

}


- Tod

From: 
<swift-evolution-boun...@swift.org<mailto:swift-evolution-boun...@swift.org>> 
on behalf of Trent Nadeau via swift-evolution 
<swift-evolution@swift.org<mailto:swift-evolution@swift.org>>
Reply-To: Trent Nadeau <tanad...@gmail.com<mailto:tanad...@gmail.com>>
Date: Thursday, May 12, 2016 at 10:52 AM
To: Rod Brown <rodney.bro...@icloud.com<mailto:rodney.bro...@icloud.com>>
Cc: Douglas Gregor via swift-evolution 
<swift-evolution@swift.org<mailto:swift-evolution@swift.org>>
Subject: Re: [swift-evolution] Optional assignment operator

This same operator (except spelled as ??=) was up as a proposal and rejected in 
February. See http://thread.gmane.org/gmane.comp.lang.swift.evolution/7694.

On Thu, May 12, 2016 at 10:41 AM, Rod Brown via swift-evolution 
<swift-evolution@swift.org<mailto:swift-evolution@swift.org>> wrote:
I’m tentatively supportive of this proposal. I definitely see the use case 
(assign only if not not nil).  Interested to hear the opinions of others here :)

-Rod


> On 12 May 2016, at 11:59 PM, Jose Manuel Sánchez Peñarroja via 
> swift-evolution <swift-evolution@swift.org<mailto:swift-evolution@swift.org>> 
> wrote:
>
> Sorry if this has already been discussed, if so I couldn’t find it.
>
> I would like to propose to add to Swift an optional assignment operator ?=
> I think this would nicely align with the other uses of ?, and avoid 
> repetition in this case:
>
>       var value = 5
>
>       var possibleNewValue: Int? = nil
>
>       value = possibleNewValue ?? value
>
> It would be used like this:
>
>       value ?= possibleNewValue
>
> I’ve found quite a few cases in which this would be very useful to me. It is 
> already possible to implement it, but having it defined in the standard 
> library would define an standard, and prevent different semantics depending 
> on who implements it.
>
>
>       infix operator ?= {
>         associativity right
>          precedence 90
>          assignment
>       }
>
>       func ?= <T>(inout lhs: T, rhs: T?) {
>           lhs = rhs ?? lhs
>       }
>
>
> Regards,
> José Manuel Sanchez
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org<mailto:swift-evolution@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution

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



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

Reply via email to