Proposal Sketch: simplify optional unwrapping syntax


(Kyle Carson) #1

I've got another solution. It's simple and very "swift". The code "var != nil" can be used as a hint to the compiler that *var* is not an optional.

Code example:

let color : Color? = getFavoriteColor()
if color != nil {
  // Color is no longer an Optional
}


(Jordan Rose) #2

This is problematic for anything other than 'let', since the value could change / be changed while inside the block. And I wouldn't want to give 'let' special privileges here.

Jordan

···

On Dec 21, 2015, at 18:36 , Kyle Carson via swift-evolution <swift-evolution@swift.org> wrote:

I've got another solution. It's simple and very "swift". The code "var != nil" can be used as a hint to the compiler that *var* is not an optional.

Code example:

let color : Color? = getFavoriteColor()
if color != nil {
  // Color is no longer an Optional
}

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


(Tino) #3

let color : Color? = getFavoriteColor()
if color != nil {
  // Color is no longer an Optional
}

well, I'd just write
if let color = getFavoriteColor() {…

This even more as the other solutions hide that "color" isn't the original "color" anymore inside the braces:
The first one was an Optional, the second one isn't.

Tino