[swift-users] comparison Non-Optional and Optional without unwrap

Alex Blewitt alblue at apple.com
Tue Oct 18 04:08:29 CDT 2016


> On 18 Oct 2016, at 09:58, Седых Александр via swift-users <swift-users at swift.org> wrote:
> 
> This code work: 
> 
> let one: Int? = 5
> let two = 5
> let result = one == two
>  
> print(result)
> 
> //print true
> 
> Why we can access to Optional value without unwrap within comparison operations?

The 'one' value isn't being unwrapped; the 'two' value is being wrapped in an optional, and then compared. In effect, it's doing:

let result = one == Optional(two)

This allows you to pass in non-optional values to functions that take optional arguments, e.g.

func printme(_ i:Int?) { print("\(i)") }

printme(one)
printme(two)

Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161018/0a6f1690/attachment.html>


More information about the swift-users mailing list