Optional assignment operator

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

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> 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
https://lists.swift.org/mailman/listinfo/swift-evolution

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> 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> 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
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

--
Trent Nadeau

We formally discussed & rejected this already:
http://article.gmane.org/gmane.comp.lang.swift.evolution/7694

-Chris

···

On May 12, 2016, at 2:38 PM, Tod Cunningham via swift-evolution <swift-evolution@swift.org> wrote:

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.

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

Yeah, as previously discussed you’d usually be better doing something like:

  struct Foo {
    var value:Int?

    func someMethod() {
      let value = self.value ?? 0 // This is no longer optional
      …
    }
  }

Since you end up with a non-optional version to work with, which this operator wouldn’t do (requiring you to unwrap it to use it anyway). To make the operator be as useful it would have to implicitly shadow the variable that it’s being assigned to, which would be strange and require some syntax of its own to be clear; in effect the ?? operator already has all the information you need as shown above, it’s a little longer but it’s more self-documenting so I think it covers the use-cases of an optional assignment pretty well.

···

On 12 May 2016, at 23:12, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On May 12, 2016, at 2:38 PM, Tod Cunningham via swift-evolution <swift-evolution@swift.org> wrote:

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.

We formally discussed & rejected this already:
http://article.gmane.org/gmane.comp.lang.swift.evolution/7694

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