[swift-evolution] Optional Assignment Operator

Nichi Shin shinnichika at icloud.com
Wed Jan 25 10:40:53 CST 2017


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).

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170125/992cff34/attachment.html>


More information about the swift-evolution mailing list