# The scope of an unwrapped optional in an optional chain

**URL:** <https://forums.swift.org/t/the-scope-of-an-unwrapped-optional-in-an-optional-chain/17410>\
**Category:** Using Swift\
**Tags:** syntax, optional\
**Created:** [October 26, 2018, 9:34pm UTC](https://forums.swift.org/t/the-scope-of-an-unwrapped-optional-in-an-optional-chain/17410 "2018-10-26T21:34:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nekno](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nekno/32/15154_2.png) [@nekno](https://forums.swift.org/u/nekno)\
**Post date:** [October 26, 2018, 9:34pm UTC](https://forums.swift.org/t/the-scope-of-an-unwrapped-optional-in-an-optional-chain/17410/1 "2018-10-26T21:34:23Z")

</div>

What is the scope/lifetime of an optional that successfully unwraps in an optional chain?

For example:

```swift
let closure = { [weak self] in
  self?.someMethod(with: self!.someProperty)
}

```

My understanding is that this executes safely at runtime because the optional chain evaluates from left to right, so `self?` unwraps in order to call `someMethod()`, and since it is retained while the method is called, `self!` is then safely unwrapped in order to pass in the argument. If `self` deallocates before the `self?` expression is evaluated, `someMethod()` isn't called and the rest of the statement isn't evaluated.

If, however, there is another value/object involved in the chain, like another property on `self`:

```swift
let closure = { [weak self] in
  self?.someProperty.someMethod(with: self!.someOtherProperty)
}

```

Is this still a "safe" statement at runtime?

Once `self?` is unwrapped, is the strong reference to the unwrapped `self` immediately released once `someProperty` is resolved, or is it held while `someProperty.someMethod()` is called?

Is there a chance that `self?` successfully unwraps, `someProperty` is retained, `self` goes out of scope and is deallocated, and `self!` now fails when `someMethod()` is executed?

Or is this previous statement equivalent to:

```swift
let closure = { [weak self] in
  if let sself = self {
    sself.someProperty.someMethod(with: sself.someOtherProperty)
  }
}

```

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [October 27, 2018, 3:08am UTC](https://forums.swift.org/t/the-scope-of-an-unwrapped-optional-in-an-optional-chain/17410/2 "2018-10-27T03:08:39Z")

</div>

Yes, that is a possibility; successfully loading a non-`nil` value from the weak capture of `self` does not extend the underlying object's lifetime beyond the code that immediately uses it.

Usually this is okay because you're not actually racing with the final release of the object. It's not really a good idea to set up a closure that races that way!

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [October 27, 2018, 7:37am UTC](https://forums.swift.org/t/the-scope-of-an-unwrapped-optional-in-an-optional-chain/17410/3 "2018-10-27T07:37:25Z")

</div>

There was a related discussion at [Confirming order of operations](https://forums.swift.org/t/confirming-order-of-operations/16376) , where [Joe Groff said](https://forums.swift.org/t/confirming-order-of-operations/16376/9):

> This isn't guaranteed. Releases may be optimized to happen earlier than this, to any point after the last formal use of the strong reference. ...

---

<div class="post-metadata">

**Author:** ![nekno](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nekno/32/15154_2.png) [@nekno](https://forums.swift.org/u/nekno)\
**Post date:** [November 4, 2018, 8:47pm UTC](https://forums.swift.org/t/the-scope-of-an-unwrapped-optional-in-an-optional-chain/17410/4 "2018-11-04T20:47:05Z")

</div>

Thanks @John_McCall and Martin — just trying to get crystal clear now, so that I can remove all doubt.

My sense is that this scenario could be slightly different than @Joe_Groff addressed, in that he's saying an optional from the left-hand side of an assignment isn't guaranteed to be retained while the right-hand side is evaluated. As krilnon [showed in the emitted SIL](https://forums.swift.org/t/confirming-order-of-operations/16376/2), while the sequence of retains and releases allows this to work (or may sometimes work) today, it isn't guaranteed.

So, is the scenario different if both references to `self` appear in the same method call, as in my example?

It seems like the answer hangs on what Joe calls "the last formal use of the strong reference".

Meaning, assuming `self?.someProperty` constitutes a formal use as a direct method call to the getter on `self`, can `self?.someProperty.someMethod()` also constitute a formal use of `self` because it appears in the same expression?

Are there any references I can consult?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [November 4, 2018, 9:27pm UTC](https://forums.swift.org/t/the-scope-of-an-unwrapped-optional-in-an-optional-chain/17410/5 "2018-11-04T21:27:07Z")

</div>

I don't know that we'd guarantee that it would work even in the same method call. Now, for all practical purposes, if the method call can't be devirtualized and optimized then there's no way it wouldn't actually work, but I think we'd always encourage people to do a single load and check instead of trying to rely on that kind of analysis.
