[swift-evolution] Optional Assignment Operator

Jacob Bandes-Storch jtbandes at gmail.com
Wed Jan 25 11:47:44 CST 2017


Really? My observation from a quick test is that "a? = b" assigns b to a if
a already has a value, or does nothing if it's nil. This is sort of the
opposite of what's being proposed, which is that "a ?= b" should assign to
a only if it does NOT have a value.
On Wed, Jan 25, 2017 at 9:33 AM Joe Groff via swift-evolution <
swift-evolution at swift.org> wrote:

>
> > On Jan 25, 2017, at 8:40 AM, Nichi Shin via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> > I’d like to propose a new operator for optional assignment in Swift.
> >
> > The idea is that by using this operator (e.g. by doing a ?= b), the
> optional on the right would be assigned to the variable on the left only
> when it has something to assign (i.e. when it's not nil).
>
> `a? = b` already does this. Maybe we need a fixit to make that more
> apparent, though.
>
> -Joe
>
> >
> > The implementation could be something as follows:
> >
> > /// Optional Assignment Operator
> > infix operator ?=: AssignmentPrecedence
> >
> > func ?=<T>(left: inout T, right: T?) {
> >     if right != nil {
> >         left = right!
> >     }
> > }
> >
> > func ?=<T>(left: inout T?, right: T?) {
> >     if right != nil {
> >         left = right
> >     }
> > }
> >
> > I hope you will consider adding this on a future release of this great
> programming language.
> >
> > Kind regards,
> > N. S.
> > _______________________________________________
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170125/6e733594/attachment.html>


More information about the swift-evolution mailing list