nested optionals

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

nested optionals

Alex Hall
Hey all,
I was running into a weird problem in an app, where I unwrapped an optional but was still getting an optional. I made a Playground to test it out, and discovered that Swift can nest optionals within optionals. Is this intended behavior?

let dict:[String:String?] = ["id":"5"]
let myInt:Int? = Int(dict["id"]!!) //note the two exclamation marks

print("myInt is \(myInt!)")

If you remove one of the two exclamation marks from the myInt line, you get an error that the optional was not unwrapped. Looks like Swift can have an optional as the non-nil value of an optional, and a single unwrap isn't enough to collapse all the optionals stored inside and give you the non-nil value at the end. I'm guessing this is how it's supposed to work, but it threw me off at first; I expected to unwrap an optional and get a non-optional value, not another optional. I can see the logic behind this way of doing things, I just never knew it existed and wanted to check that this is, in fact, how it's supposed to work.

--
Have a great day,
Alex Hall
[hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/329819FD-5168-44DF-A637-3C6F6A4BA073%40icloud.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: nested optionals

Daniel T.
Yes this is expected behavior. You got a monad in your monad! :-) 

You have this: 

let dict: Dictionary<String, Optional<String>> = ["id": "5"]
let item: Optional<Optional<String>> = dict["id"]


On Sep 15, 2015, at 9:19 AM, Alex Hall <[hidden email]> wrote:

Hey all,
I was running into a weird problem in an app, where I unwrapped an optional but was still getting an optional. I made a Playground to test it out, and discovered that Swift can nest optionals within optionals. Is this intended behavior?

let dict:[String:String?] = ["id":"5"]
let myInt:Int? = Int(dict["id"]!!) //note the two exclamation marks

print("myInt is \(myInt!)")

If you remove one of the two exclamation marks from the myInt line, you get an error that the optional was not unwrapped. Looks like Swift can have an optional as the non-nil value of an optional, and a single unwrap isn't enough to collapse all the optionals stored inside and give you the non-nil value at the end. I'm guessing this is how it's supposed to work, but it threw me off at first; I expected to unwrap an optional and get a non-optional value, not another optional. I can see the logic behind this way of doing things, I just never knew it existed and wanted to check that this is, in fact, how it's supposed to work.

--
Have a great day,
Alex Hall
[hidden email]


--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/329819FD-5168-44DF-A637-3C6F6A4BA073%40icloud.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/3777A7C4-D145-4755-A9E3-64B16CC35CD7%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: nested optionals

Daniel T.
In reply to this post by Alex Hall
Fun with flatMap!

let dict:[String: String?] = ["id": "5"]
let myInt = dict["id"].flatMap { $0.flatMap { Int($0) } }
print("myInt is \(myInt!)")


On Sep 15, 2015, at 9:19 AM, Alex Hall <[hidden email]> wrote:

Hey all,
I was running into a weird problem in an app, where I unwrapped an optional but was still getting an optional. I made a Playground to test it out, and discovered that Swift can nest optionals within optionals. Is this intended behavior?

let dict:[String:String?] = ["id":"5"]
let myInt:Int? = Int(dict["id"]!!) //note the two exclamation marks

print("myInt is \(myInt!)")

If you remove one of the two exclamation marks from the myInt line, you get an error that the optional was not unwrapped. Looks like Swift can have an optional as the non-nil value of an optional, and a single unwrap isn't enough to collapse all the optionals stored inside and give you the non-nil value at the end. I'm guessing this is how it's supposed to work, but it threw me off at first; I expected to unwrap an optional and get a non-optional value, not another optional. I can see the logic behind this way of doing things, I just never knew it existed and wanted to check that this is, in fact, how it's supposed to work.

--
Have a great day,
Alex Hall
[hidden email]


--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/329819FD-5168-44DF-A637-3C6F6A4BA073%40icloud.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/3C9828C3-BAE2-4A88-9E91-0E348D385334%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: nested optionals

Swift Language mailing list
In reply to this post by Alex Hall
To me it looks like it is as expected.  The value type in the dictionary is Optional<String>  The subscript return value is the value type of the dictionary wrapped in an Optional i.e. Optional<Optional<String>>

Why would you expect anything else?

> On 15 Sep 2015, at 14:19, Alex Hall <[hidden email]> wrote:
>
> Hey all,
> I was running into a weird problem in an app, where I unwrapped an optional but was still getting an optional. I made a Playground to test it out, and discovered that Swift can nest optionals within optionals. Is this intended behavior?
>
> let dict:[String:String?] = ["id":"5"]
> let myInt:Int? = Int(dict["id"]!!) //note the two exclamation marks
>
> print("myInt is \(myInt!)")
>
> If you remove one of the two exclamation marks from the myInt line, you get an error that the optional was not unwrapped. Looks like Swift can have an optional as the non-nil value of an optional, and a single unwrap isn't enough to collapse all the optionals stored inside and give you the non-nil value at the end. I'm guessing this is how it's supposed to work, but it threw me off at first; I expected to unwrap an optional and get a non-optional value, not another optional. I can see the logic behind this way of doing things, I just never knew it existed and wanted to check that this is, in fact, how it's supposed to work.
>
> --
> Have a great day,
> Alex Hall
> [hidden email]
>
>
> --
> You received this message because you are subscribed to the Google Groups "Swift Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/329819FD-5168-44DF-A637-3C6F6A4BA073%40icloud.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/6034C920-EF02-4411-B9E9-150FA190CB6A%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: nested optionals

Alex Hall

> On Sep 16, 2015, at 12:39, Jeremy Pereira <[hidden email]> wrote:
>
> To me it looks like it is as expected.  The value type in the dictionary is Optional<String>  The subscript return value is the value type of the dictionary wrapped in an Optional i.e. Optional<Optional<String>>
>
> Why would you expect anything else?

As I said, I get the logic, I just thought I'd check. The way I learned it, an optional can have either a value or nil, which is the whole point of optionals. No one ever told me that the non-nil value in an optional can be another optional, so I figured it could be relied on to always be a non-optional. I'd never seen it before, and had never run across it in reading anything or talking to others about Swift, so I never expected it to work that way. Basically, I just didn't realize that Swift supports letting an optional be the non-nil value of an optional. I wanted to check to be sure it wasn't something odd, or that I wasn't doing something wrong.

>
>> On 15 Sep 2015, at 14:19, Alex Hall <[hidden email]> wrote:
>>
>> Hey all,
>> I was running into a weird problem in an app, where I unwrapped an optional but was still getting an optional. I made a Playground to test it out, and discovered that Swift can nest optionals within optionals. Is this intended behavior?
>>
>> let dict:[String:String?] = ["id":"5"]
>> let myInt:Int? = Int(dict["id"]!!) //note the two exclamation marks
>>
>> print("myInt is \(myInt!)")
>>
>> If you remove one of the two exclamation marks from the myInt line, you get an error that the optional was not unwrapped. Looks like Swift can have an optional as the non-nil value of an optional, and a single unwrap isn't enough to collapse all the optionals stored inside and give you the non-nil value at the end. I'm guessing this is how it's supposed to work, but it threw me off at first; I expected to unwrap an optional and get a non-optional value, not another optional. I can see the logic behind this way of doing things, I just never knew it existed and wanted to check that this is, in fact, how it's supposed to work.
>>
>> --
>> Have a great day,
>> Alex Hall
>> [hidden email]
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Swift Language" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
>> To post to this group, send email to [hidden email].
>> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/329819FD-5168-44DF-A637-3C6F6A4BA073%40icloud.com.
>> For more options, visit https://groups.google.com/d/optout.
>


--
Have a great day,
Alex Hall
[hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/71E7C69A-B38D-4116-93A9-70D28C910056%40icloud.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: nested optionals

Swift Language mailing list

> On 16 Sep 2015, at 17:59, Alex Hall <[hidden email]> wrote:
>
>
>> On Sep 16, 2015, at 12:39, Jeremy Pereira <[hidden email]> wrote:
>>
>> To me it looks like it is as expected.  The value type in the dictionary is Optional<String>  The subscript return value is the value type of the dictionary wrapped in an Optional i.e. Optional<Optional<String>>
>>
>> Why would you expect anything else?
>
> As I said, I get the logic, I just thought I'd check. The way I learned it, an optional can have either a value or nil, which is the whole point of optionals. No one ever told me that the non-nil value in an optional can be another optional, so I figured it could be relied on to always be a non-optional. I'd never seen it before, and had never run across it in reading anything or talking to others about Swift, so I never expected it to work that way. Basically, I just didn't realize that Swift supports letting an optional be the non-nil value of an optional. I wanted to check to be sure it wasn't something odd, or that I wasn't doing something wrong.

Yes I can see your point of view, but I’ve always looked at Optional as just a type (in fact it is a simple generic enum with two cases) and some syntactic sugar which is why its actual behaviour came as no surprise to me.


>
>
> --
> Have a great day,
> Alex Hall
> [hidden email]
>

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/605C0B0B-919D-4BDE-80F4-242B4AA906C5%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.