[swift-evolution] Optional assignment operator

Tod Cunningham tcunningham at vectorform.com
Thu May 12 16:38:13 CDT 2016


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-bounces at swift.org<mailto:swift-evolution-bounces at swift.org>> on behalf of Trent Nadeau via swift-evolution <swift-evolution at swift.org<mailto:swift-evolution at swift.org>>
Reply-To: Trent Nadeau <tanadeau at gmail.com<mailto:tanadeau at gmail.com>>
Date: Thursday, May 12, 2016 at 10:52 AM
To: Rod Brown <rodney.brown6 at icloud.com<mailto:rodney.brown6 at icloud.com>>
Cc: Douglas Gregor via swift-evolution <swift-evolution at swift.org<mailto:swift-evolution at 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 at swift.org<mailto:swift-evolution at 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 at swift.org<mailto:swift-evolution at 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 at swift.org<mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution

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



--
Trent Nadeau


More information about the swift-evolution mailing list