# Swift 6: reconsider @escaping for optional function-type parameters

**URL:** <https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932>\
**Category:** Discussion\
**Created:** [December 8, 2021, 8:44pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932 "2021-12-08T20:44:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [December 8, 2021, 8:44pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/1 "2021-12-08T20:44:14Z")

</div>

This topic has come up before, but because of the source compatibility considerations I wanted to raise it again in the context of Swift 6 and the possibility of a breaking change in a new language mode. The motivation and solution are the same as discussed [here](https://forums.swift.org/t/allowing-explicit-escaping-for-optional-closures-in-function-parameters/28334). Broadly we would change the default behavior of optional function-typed function parameters so that in:

```swift
func foo(fn: (() -> Void)?) {}

```

the parameter `fn` would be non-escaping, and would require the addition of an `@escaping` attribute to make it escaping.

This would apply only to optional function types (perhaps at any level of optional nesting?) and would not imply any broader form of escape analysis for other values/references. In the Swift 5 language mode, `@escaping` would continue be the implicit default and there would be no way to spell a non-escaping optional function-typed parameter.

Previous discussions have indicated that such a change would be [highly feasible](https://forums.swift.org/t/unrequired-in-function-signature-for-optional-closure-parameters/27577/48), so it seems that the only remaining question to answers is whether this change is worth a source break in a new language version. With the opportunity upon us, such a discussion seems timely. Curious to hear everyone's thoughts!

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [December 8, 2021, 9:24pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/2 "2021-12-08T21:24:26Z")

</div>

+1  
I’ve always found it curious and awkward that optional args are automatically escaping.

As it stands it just increases cognitive load and seems inconsistent.

Also annoying when I change a parameter from non optional to optional or the other way around.

---

<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:** [December 8, 2021, 9:29pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/3 "2021-12-08T21:29:29Z")

</div>

I'm not sure it's tenable to not have a way to spell this in Swift 5 mode, but I agree that making this change would be highly desirable for Swift 6.

---

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [December 8, 2021, 10:20pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/4 "2021-12-08T22:20:21Z")

</div>

I’m obviously in favor of this change as original pitcher. 🙂 I’ll be happy to contribute on its implementation as much as i can

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [December 9, 2021, 4:07pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/5 "2021-12-09T16:07:41Z")

</div>

I'm +1 on this pitch, but think that this is a more general issue. Consider a type like `Either`:

```swift
enum Either<A, B> { case a(A), b(B) }

```

There isn't a significant structural difference between `Optional` and such a type — they're both generic enums that accept payload on their cases. Thus, an `@noescape`-type attribute for types whose payload is known not to escape seems generally useful. (I'm not sure if this is the feature [described](https://forums.swift.org/t/unrequired-in-function-signature-for-optional-closure-parameters/27577/48) by @John_McCall or a feature that has been discussed elsewhere, so please let me know if that's the case.)

Admittedly, the sugar around optionals changes things. However, we could first introduce a general feature. This way, the implicit no-escape syntax of optional-wrapped function parameters would build on an existing feature, instead of being a niche `Optional` feature.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [December 9, 2021, 4:30pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/6 "2021-12-09T16:30:53Z")

</div>

A general feature would be great, but I suspect significantly more complicated than the optional version of this feature, for significantly less benefit. In Swift codebases I've worked in, a large number of closures are passed as optional function-type parameters, and situations where they are assigned to, say, enum cases or struct properties without ultimately escaping are vanishingly rare.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [December 9, 2021, 4:48pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/7 "2021-12-09T16:48:28Z")

</div>

You could extend it even further - to stored properties in struct and classes. Why should enums be so special?

Basically, the general feature is something like Rust's lifetime system. You have a closure, with a certain lifetime (limited to the function it was defined in), and anything which stores that closure - whether it's an enum like `Optional` or `Either`, or any other kind of storage - is also bound by that same lifetime and cannot extend it by "escaping".

That said, `Optional` is semi-compiler-magic anyway, so we might be able to make this one (very common) case a bit easier without a fully-general lifetime system.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [December 9, 2021, 7:17pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/8 "2021-12-09T19:17:25Z")

</div>

I'm kind of horrified to find out that Optional function type parameters are implicitly escaping. This is the first I'm hearing of this. The only thing I would have expected to auto escape would be a function declared outside of the thing taking a function parameter.

Indeed, Optional and escaping seem orthogonal.

+1 to this.

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [December 16, 2021, 9:43pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/9 "2021-12-16T21:43:31Z")

</div>

> Indeed, Optional and escaping seem orthogonal.

+1 to the proposal, based on the logic in this comment from @Hacksaw.

This also follows the [Principle of Least Astonishment/Surprise](https://en.wikipedia.org/wiki/Principle_of_least_astonishment).

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [December 17, 2021, 6:05am UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/10 "2021-12-17T06:05:42Z")

</div>

+1 for me. I've been bitten by this in the past where I had to realize the implicit `@escaping` behavior for generic parameters the hard way.

```swift
func foo<T>(_ value: T) // `T` is implicitly @escaping

// hence `Optional<Wrapped>.init(...)` has an implicitly @escaping `Wrapped`
// hence `(() -> Void)?` is implicitly @escaping as well

```

I also second that it would be cool to explore a more general approach how to solve this problem everywhere while I'm okay if we start just with an `Optional`.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [December 24, 2021, 9:26pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/11 "2021-12-24T21:26:42Z")

</div>

For the sake of performance and optimal default behavior, I think we should consider eventually making **everything** non-escaping by default. Yes, including non-closure values.

Not now, obviously. Not even in Swift 6. But maybe in Swift 7?

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [March 29, 2024, 4:26pm UTC](https://forums.swift.org/t/swift-6-reconsider-escaping-for-optional-function-type-parameters/53932/12 "2024-03-29T16:26:30Z")

</div>

+1 for it.

I wonder should it be done with relation to [[Pitch] Non-Escapable Types and Lifetime Dependency](https://forums.swift.org/t/pitch-non-escapable-types-and-lifetime-dependency/69865)

Seems that `enum Either<A, B>` and similar types can also keep `noescapable` closures. This way `Optional<T>` will be a particular case.
