[swift-users] Optional binding with non-optional expression

Martin R martinr448 at gmail.com
Fri Jun 2 07:40:33 CDT 2017

This following code fails to compile (which is correct, as far as I can judge that):

   if let x = 5 { }
   // error: initializer for conditional binding must have Optional type, not 'Int'

But why is does it compile (with a warning) if an explicit type annotation is added?

   if let y: Int = 5 { }
   // warning: non-optional expression of type 'Int' used in a check for optionals

Tested with Xcode 8.3.2 and both the build-in Swift 3.1 toolchain and the Swift 4.0 snapshot from May 25, 2017.

I am just curious and would like to understand if there is fundamental difference between those statements.

Regards, Martin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170602/b17ae5fb/attachment.html>

More information about the swift-users mailing list