# \[Pitch #2\] Called-once function types

**URL:** <https://forums.swift.org/t/pitch-2-called-once-function-types/90515>\
**Category:** Pitches\
**Created:** [November 5, 2026, 10:01pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515 "2026-11-05T22:01:00Z")\
**Posts on this page:** 8\
**Page:** 3

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [November 13, 2026, 5:26pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/41 "2026-11-13T17:26:34Z")

</div>

> [@tshortli](#):
>
> > [@FranzBusch](#):
> >
> > It is not directly spelled out so just making sure, there will also be an `async` variant of this method which is `nonisolated(nonsending)` and where the closure is also `async` + `nonisolated(nonsending)` right?
> 
> I hadn't thought about the need for an async overload. Yes, I'll add this to the proposal, thanks!

It turns out a separate overload isn't needed. Like `withoutActuallyEscaping(_:do:)`, the compiler type checks calls to `withoutActuallyDeiniting(_:do:)` specially, and it treats the function as `async` when the body is `async`. The body closure's isolation is inferred from its context, like any other non-escaping closure, so the call never switches executors. With `NonisolatedNonsendingByDefault` enabled, the body of a call in a `nonisolated(nonsending)` function is `nonisolated(nonsending)` too.

I will clarify this in the proposal.

---

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [November 13, 2026, 9:50pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/42 "2026-11-13T21:50:18Z")

</div>

I have pushed a 3rd [revision](https://github.com/swiftlang/swift-evolution/pull/3429/changes/4801d31745cc41a877545a29f3183becf61f5229) of the proposal to address feedback in this thread:

- `borrowing` is now allowed on `@once` and `@callable(once)` types and the implications of `inout` are discussed.
- The requirements of linear types are clarified (the must be both `Copyable` and `Deinitable`).
- The invertible marker protocol declarations in the standard library, including `Deinitable`, are now included.
- Support for async closures passed to `withoutActuallyDeiniting(_:do:)` is discussed.
- The nitty-gritty details of overloading rules have been added to cover important source compatibility considerations. Also, examples of how libraries can successfully offer overloads on execution semantics is now covered in detail.
- An alternative considered was added for importing nullable `called_once` parameters as optional `@callable(once)`.

---

<div class="post-metadata">

**Author:** ![bobko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bobko/32/37854_2.png) [@bobko](https://forums.swift.org/u/bobko)\
**Post date:** [November 14, 2026, 1:52pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/43 "2026-11-14T13:52:35Z")

</div>

Does this proposal address `defer` blocks as well? [Consuming Noncopyable in defer block · Issue #76388 · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/issues/76388)

I never realized that `defer` blocks were closures. I assumed that they were syntactic blocks (like `if` blocks). But maybe it's some implementation detail leaking.

Or do we consider it a separate bug, since `defer` shouldn't be closures?

---

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [November 15, 2026, 3:32pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/44 "2026-11-15T15:32:47Z")

</div>

It's true that an implementation of this proposal would unblock fixing that issue, but I don't think it should be explicitly called out in the proposal. Modeling `defer` as a closure is an implementation detail of the compiler rather than a language design choice.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [November 15, 2026, 4:35pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/45 "2026-11-15T16:35:20Z")

</div>

> [@tshortli](#):
>
> Modeling `defer` as a closure is an implementation detail of the compiler rather than a language design choice.

Actually this reminds me, if you have a local function that is only called and not used as a value:

```swift
func f() { ... }

f()

```

What we do today is basically equivalent to forming a closure and immediately calling it.

Should we say that the type of the closure that we form when you write `f` there be called-once? It would then follow that `f()` can consume non-copyable captures and so on, as long as the calls to `f()` are in the right spots.

It would then be reasonable to just _specify_ the behavior of `defer` as being like a local function that is called at each exit point. Today the actual behavior of `defer` is not documented anywhere as far as I'm aware, and it would be easier to document it as a special kind of local function than to have to fully describe every possible interaction with every other language feature.

---

<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 15, 2026, 6:08pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/46 "2026-11-15T18:08:47Z")

</div>

> [@Slava\_Pestov](#):
>
> Should we say that the type of the closure that we form when you write `f` there be called-once? It would then follow that `f()` can consume non-copyable captures and so on, as long as the calls to `f()` are in the right spots.

You’re suggesting that we delay checking capture rules of local functions until we can analyze their uses? That’s probably feasible. But we can’t really reason about whether there’s exactly one call site on every path in the typechecker, so I’m not sure how much we could take advantage of the delay.

Probably the more meaningful impact is that we could let type information from use sites flow back into the function — maybe not parameter or result types, but at least things like that the function is required to be escaping or sendable. But again I don’t know how much this would really help because I think we can infer conflicting constraints from captures just as well. Maybe there’s a case I’m not thinking of.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [November 15, 2026, 7:40pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/47 "2026-11-15T19:40:00Z")

</div>

> [@John\_McCall](#):
>
> You’re suggesting that we delay checking capture rules of local functions until we can analyze their uses?

Ah, never mind. I thought we already did that for captures, but maybe it's not nearly as general as I'm imagining. At least for escaping vs non-escaping uses, there's a distinction though:

```swift
func f(_ fn: () -> ()) -> () -> () {
  func g1() {
    fn() // this seems to work?
  }

  func g2() {
    fn() // we diagnose this
  }

  g1()
  return g2
}

```

---

<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 15, 2026, 7:47pm UTC](https://forums.swift.org/t/pitch-2-called-once-function-types/90515/48 "2026-11-15T19:47:36Z")

</div>

I think the current rule is that we restrict the type of the function by its captures: if it has an `inout` or `~Escapable` capture, we infer it as non-escaping; if it has non-`Sendable` captures, we infer it as non-`Sendable` (regardless of RBI); etc. There’s no reverse flow.

[Previous page](https://forums.swift.org/t/pitch-2-called-once-function-types/90515.md?page=2)
