# Optional type in Swift have problem with chaining

**URL:** <https://forums.swift.org/t/optional-type-in-swift-have-problem-with-chaining/18177>\
**Category:** Using Swift\
**Tags:** swift4\
**Created:** [November 21, 2018, 10:29am UTC](https://forums.swift.org/t/optional-type-in-swift-have-problem-with-chaining/18177 "2018-11-21T10:29:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhatlee](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@nhatlee](https://forums.swift.org/u/nhatlee)\
**Post date:** [November 21, 2018, 10:29am UTC](https://forums.swift.org/t/optional-type-in-swift-have-problem-with-chaining/18177/1 "2018-11-21T10:29:57Z")

</div>

Hi all,  
I have code like below:

```
     extension Optional where Wrapped == Int {
            func toBool() -> Bool {
                guard let sSelf = self else { return false }
                return sSelf > 0
            }
        }
    
        protocol ConfigProtocol {
            var isEnable: Int { get }
        }
        class ConfigA: ConfigProtocol {
            var isEnable: Int = 0
        }

    class Foo {
        func getConfig() -> ConfigProtocol? {
            return ConfigA()
        }
    }

```

I use this code above:

```
    let foo = Foo()
    print("Type is:\(type(of: configValue))")

```

The result of print is **Type is:Optional\<Int\>**.  
But if I call `let isEnableOfInt = foo.getConfig()?.isEnable.toBool()` will get the error:  
`Value of type 'Int' has no member 'toBool'`  
**_So confuse_** : it's `Optional` not `Int` ?  
But if I change to:

```
let configValue = foo.getConfig()?.isEnable
print(configValue.toBool()) //false

```

It worked properly. Is this a bug with Optional?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [November 21, 2018, 10:45am UTC](https://forums.swift.org/t/optional-type-in-swift-have-problem-with-chaining/18177/2 "2018-11-21T10:45:26Z")

</div>

Hello @nhatlee

You get this error because in the optional chain, `isEnable` has the type `Int`, which is not `Optional<Int>`, and has no `toBool` method:

```swift
// WRONG
let isEnableOfInt = foo.getConfig()?.isEnable.toBool()
// Int ^

```

You can avoid this error by "escaping" the optional chaining with parenthesis, so that you get the `Optional<Int>` you want:

```swift
// OK
let isEnableOfInt = (foo.getConfig()?.isEnable).toBool()
// Optional<Int> ^

```

---

<div class="post-metadata">

**Author:** ![nhatlee](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@nhatlee](https://forums.swift.org/u/nhatlee)\
**Post date:** [November 21, 2018, 10:57am UTC](https://forums.swift.org/t/optional-type-in-swift-have-problem-with-chaining/18177/3 "2018-11-21T10:57:49Z")

</div>

Thanks for your help. Your solution worked. But I still feel confused with the result print of it's type: `Type is:Optional<Int>`. With the type is `Optional` then the developer should not use parenthesis for this case to get the code work.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [November 21, 2018, 11:09am UTC](https://forums.swift.org/t/optional-type-in-swift-have-problem-with-chaining/18177/4 "2018-11-21T11:09:40Z")

</div>

Please consider this other optional chain:

```swift
// A valid optional chain
let value = foo.bar?.baz.qux.blah.value // An optional value

```

In this optional chain, there is a single optional property, which is `bar`. All other properties are non-optional. Yet, thanks to optional chaining, we have to write `?` only once, on the only optional property.

If what you ask for were implemented, then all elements in the chain would be optionals. Why not? Because we'd have to write instead:

```swift
// A not very nice optional chain
let value = foo.bar?.baz?.qux?.blah?.value // An optional value

```

The Swift designers have decided that this was not very good. Not only do you have to write many `?`, but also you lose information: you don't quite know which properties are optionals, and which properties are not.

I tend to think that you will eventually agree with the language designers.

The consequence is that we have to learn that optional chains preserve the types of all members in the chain, optional or not, with the _only_ exception of the last one, the value which goes out form the chain: this one is always an optional.

And today you have learned that optional chains can be wrapped in parenthesis, so that you can force an optional wherever you want.

So you get:

- nice syntax
- ability to finely control your optionals when you need to

This looks like a fine tool to me :-)
