# \#closure

**URL:** https://forums.swift.org/tag/closure/195.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [Labeled trailing closures](https://forums.swift.org/t/labeled-trailing-closures/90202)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 13\
**Last updated:** [October 23, 2026, 9:01am UTC](https://forums.swift.org/t/labeled-trailing-closures/90202 "2026-10-23T09:01:29Z")

</div>

Is there any appetite for allowing argument labels to be applied to the first trailing closure of a call expression? As things are now, we have to choose between indenting all closure arguments: presentDeletionModal( …

---

## [Mysterious syntax in closure head](https://forums.swift.org/t/mysterious-syntax-in-closure-head/87523)

<div class="topic-metadata">

**Author:** [@ibex](https://forums.swift.org/u/ibex)\
**Replies:** 2\
**Last updated:** [June 20, 2026, 2:23pm UTC](https://forums.swift.org/t/mysterious-syntax-in-closure-head/87523 "2026-06-20T14:23:39Z")

</div>

I have stumbled on a mysterious syntax, which I don't understand. Given func f (u: @MainActor @Sendable @escaping (Int) -\> Int) async { let v = await u (Int.random (in: 0..\<1024)) print ("f (\\(v)) =", Int.random …

---

## [Assuming I handle them correctly, what closure attributes can I safely bit cast in and away?](https://forums.swift.org/t/assuming-i-handle-them-correctly-what-closure-attributes-can-i-safely-bit-cast-in-and-away/87427)

<div class="topic-metadata">

**Author:** [@atifdev10](https://forums.swift.org/u/atifdev10)\
**Replies:** 4\
**Last updated:** [June 16, 2026, 4:36pm UTC](https://forums.swift.org/t/assuming-i-handle-them-correctly-what-closure-attributes-can-i-safely-bit-cast-in-and-away/87427 "2026-06-16T16:36:39Z")

</div>

As the title says, what closure attributes can I safely bit cast in and away? @escaping @Sendable @isolated(any) any explicit isolation And do any of these have irreversible drawbacks? For example, does casting @isol…

---

## [Why can't I store this \`sending\` parameter in my mutex?](https://forums.swift.org/t/why-cant-i-store-this-sending-parameter-in-my-mutex/85449)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 15\
**Last updated:** [April 3, 2026, 2:52am UTC](https://forums.swift.org/t/why-cant-i-store-this-sending-parameter-in-my-mutex/85449 "2026-04-03T02:52:06Z")

</div>

Can anyone shed some deeper light on why the following code doesn't compile? import Foundation import Synchronization final class A { let state = Mutex(State()) struct State { var callbacks: \[UUID:…

---

## [Closure isolation capture seemingly can't see through aliases – expected or a bug?](https://forums.swift.org/t/closure-isolation-capture-seemingly-cant-see-through-aliases-expected-or-a-bug/77850)

<div class="topic-metadata">

**Author:** [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Replies:** 3\
**Last updated:** [February 6, 2026, 1:48am UTC](https://forums.swift.org/t/closure-isolation-capture-seemingly-cant-see-through-aliases-expected-or-a-bug/77850 "2026-02-06T01:48:04Z")

</div>

closures can currently be isolated in two ways: via a gobal-actor-isolated type annotation via a capture of an isolated parameter for the second case, i was a bit surprised to find that certain forms of isolated param…

---

## [Possible major bug in parameter packs when used with closures](https://forums.swift.org/t/possible-major-bug-in-parameter-packs-when-used-with-closures/83958)

<div class="topic-metadata">

**Author:** [@ofgokce](https://forums.swift.org/u/ofgokce)\
**Replies:** 3\
**Last updated:** [January 8, 2026, 11:31pm UTC](https://forums.swift.org/t/possible-major-bug-in-parameter-packs-when-used-with-closures/83958 "2026-01-08T23:31:46Z")

</div>

I do not know whether this category is right or not but I just had a minor and a major problem regarding the parameter packs. The minor one is that the @autoclosure annotation is being ignored when being used with repea…

---

## [Compiler issue: Escaping closure captures non-escaping parameter 'block'](https://forums.swift.org/t/compiler-issue-escaping-closure-captures-non-escaping-parameter-block/83578)

<div class="topic-metadata">

**Author:** [@blazej.sleboda](https://forums.swift.org/u/blazej.sleboda)\
**Replies:** 13\
**Last updated:** [December 10, 2025, 11:22am UTC](https://forums.swift.org/t/compiler-issue-escaping-closure-captures-non-escaping-parameter-block/83578 "2025-12-10T11:22:57Z")

</div>

How can we explain why func executeNOK doesn’t compile but execute1 and executeOk does? func executeOk(block: () -\> Void) { { block() }() } func execute1(block: () -\> Void) { let closure = {…

---

## [Casting closure collections](https://forums.swift.org/t/casting-closure-collections/82100)

<div class="topic-metadata">

**Author:** [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Replies:** 11\
**Last updated:** [November 4, 2025, 9:22am UTC](https://forums.swift.org/t/casting-closure-collections/82100 "2025-11-04T09:22:42Z")

</div>

I'm working on a fix for #82618. For us this is a blocker for incremental Swift 6 adoption, but it also reproduces without concurrency features. The bug can be fixed in different ways: Rejecting the code with an compi…

---

## [Closure based configuration](https://forums.swift.org/t/closure-based-configuration/82215)

<div class="topic-metadata">

**Author:** [@Roshankumar350](https://forums.swift.org/u/Roshankumar350)\
**Replies:** 17\
**Last updated:** [September 24, 2025, 4:31pm UTC](https://forums.swift.org/t/closure-based-configuration/82215 "2025-09-24T16:31:16Z")

</div>

Motivation: While the Swift Book does an excellent job introducing closure expressions, trailing closures, and functional patterns like sorting and mapping, it currently lacks coverage of a practical and idiomatic use c…

---

## [Capture noncopyable in closure](https://forums.swift.org/t/capture-noncopyable-in-closure/80460)

<div class="topic-metadata">

**Author:** [@hguandl](https://forums.swift.org/u/hguandl)\
**Replies:** 1\
**Last updated:** [June 14, 2025, 6:27am UTC](https://forums.swift.org/t/capture-noncopyable-in-closure/80460 "2025-06-14T06:27:08Z")

</div>

Hi there, I'm experimenting with noncopyable types in Swift and encountered a problem when trying to consume a value within a closure. Here's a minimal reproduction: struct Foo: ~Copyable { } func withBlock(\_ block: ()…

---

## [Merging Closure into Protocols](https://forums.swift.org/t/merging-closure-into-protocols/71499)

<div class="topic-metadata">

**Author:** [@aetherealtech](https://forums.swift.org/u/aetherealtech)\
**Replies:** 10\
**Last updated:** [May 18, 2025, 9:53pm UTC](https://forums.swift.org/t/merging-closure-into-protocols/71499 "2025-05-18T21:53:33Z")

</div>

This is an idea that’s been in my mind for years. I’m curious if the language team has ever considered it too. I find it useful to imagine that, under the hood, function types are syntactic sugar for protocols. For ex…

---

## [Avoiding “Overlapping Access” errors](https://forums.swift.org/t/avoiding-overlapping-access-errors/47320)

<div class="topic-metadata">

**Author:** [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Replies:** 9\
**Last updated:** [May 2, 2025, 5:49pm UTC](https://forums.swift.org/t/avoiding-overlapping-access-errors/47320 "2025-05-02T17:49:21Z")

</div>

I'm writing a pure-Swift GeoTIFF parser. Part of that involves creating a BinaryReader struct that maintains an index into a Data, and allows seeking and reading different types. Something I commonly do is save the curr…

---

## [Weak captures in sendable/sending closures](https://forums.swift.org/t/weak-captures-in-sendable-sending-closures/78498)

<div class="topic-metadata">

**Author:** [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Replies:** 20\
**Last updated:** [April 15, 2025, 8:41am UTC](https://forums.swift.org/t/weak-captures-in-sendable-sending-closures/78498 "2025-04-15T08:41:50Z")

</div>

Consider the following example: final class X: Sendable { func bar() {} } func doIt(\_ block: () -\> Void) { block() } func foo() { let x = X() doIt { \[weak x\] in Task { // error: passing closure…

---

## [Differentiable error](https://forums.swift.org/t/differentiable-error/78884)

<div class="topic-metadata">

**Author:** [@gonsolo](https://forums.swift.org/u/gonsolo)\
**Replies:** 0\
**Last updated:** [March 27, 2025, 5:27pm UTC](https://forums.swift.org/t/differentiable-error/78884 "2025-03-27T17:27:52Z")

</div>

Hi! Can someone explain to me why this doesn't work and what the error message is about? import \_Differentiation func f(x: Double) -\> Double { return x \* x } print(gradient(at: 4.0, of: f)) …

---

## [Closure Isolation Inheritance Issues](https://forums.swift.org/t/closure-isolation-inheritance-issues/78703)

<div class="topic-metadata">

**Author:** [@eli](https://forums.swift.org/u/eli)\
**Replies:** 0\
**Last updated:** [March 21, 2025, 12:19am UTC](https://forums.swift.org/t/closure-isolation-inheritance-issues/78703 "2025-03-21T00:19:35Z")

</div>

Hello, Im trying to create a Queue, which allows me to synchronise execution despite having a reentry point. Doing the above I ran into few problems. Basically I have a simple actor public typealias RoutingQueueOperat…

---

## [Explicit self not required for Task closures?](https://forums.swift.org/t/explicit-self-not-required-for-task-closures/54364)

<div class="topic-metadata">

**Author:** [@Drew\_Crawford1](https://forums.swift.org/u/Drew_Crawford1)\
**Replies:** 20\
**Last updated:** [March 3, 2025, 1:10pm UTC](https://forums.swift.org/t/explicit-self-not-required-for-task-closures/54364 "2025-03-03T13:10:27Z")

</div>

I think this is a bug, but I would like confirmation. The following code compiles cleanly, whereas I think it should error due to lack of explicit self final class Foo { var thisVariableInvolvesSelf = 42 f…

---

## [Do empty closures allocate anything in Swift?](https://forums.swift.org/t/do-empty-closures-allocate-anything-in-swift/77694)

<div class="topic-metadata">

**Author:** [@isaac-weisberg](https://forums.swift.org/u/isaac-weisberg)\
**Replies:** 6\
**Last updated:** [February 6, 2025, 2:55pm UTC](https://forums.swift.org/t/do-empty-closures-allocate-anything-in-swift/77694 "2025-02-06T14:55:56Z")

</div>

I am going to call a function: func foo(\_ completion: @escaping () -\> Void) with foo { } Question: does { } allocate anything? There are no instructions, no captures, no parameters, so it might as well be described …

---

## [Long-term solution for accidental retain cycles from strong references in closures](https://forums.swift.org/t/long-term-solution-for-accidental-retain-cycles-from-strong-references-in-closures/77201)

<div class="topic-metadata">

**Author:** [@moreindirection](https://forums.swift.org/u/moreindirection)\
**Replies:** 15\
**Last updated:** [January 20, 2025, 11:42am UTC](https://forums.swift.org/t/long-term-solution-for-accidental-retain-cycles-from-strong-references-in-closures/77201 "2025-01-20T11:42:42Z")

</div>

In general, I'm a huge fan of Swift. I really appreciate its goal of being a safe language. Swift does its best to make sure programs that pass the compiler won't unsafely access memory, have data races or crash from nul…

---

## [Errors passing unapplied function references as closures](https://forums.swift.org/t/errors-passing-unapplied-function-references-as-closures/77112)

<div class="topic-metadata">

**Author:** [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Replies:** 1\
**Last updated:** [January 10, 2025, 10:02am UTC](https://forums.swift.org/t/errors-passing-unapplied-function-references-as-closures/77112 "2025-01-10T10:02:30Z")

</div>

In Swift, we can normally pass a function reference as an argument to a function that expects a closure. However, in many cases that fails or produces warnings. Here are some: Mutating function references: let items = …

---

## [Conditionally conform to Sendable when closure property is Sendable](https://forums.swift.org/t/conditionally-conform-to-sendable-when-closure-property-is-sendable/70629)

<div class="topic-metadata">

**Author:** [@roanutil](https://forums.swift.org/u/roanutil)\
**Replies:** 13\
**Last updated:** [December 9, 2024, 11:57pm UTC](https://forums.swift.org/t/conditionally-conform-to-sendable-when-closure-property-is-sendable/70629 "2024-12-09T23:57:31Z")

</div>

My understanding is that closures are treated a little differently and can't conditionally be Sendable. Let's say I have a type that holds onto a closure: public struct Effect\<Input, Output\> { public typealias Clos…

---

## [Value wrapped in a thunk recursively(?)](https://forums.swift.org/t/value-wrapped-in-a-thunk-recursively/75925)

<div class="topic-metadata">

**Author:** [@ryder](https://forums.swift.org/u/ryder)\
**Replies:** 4\
**Last updated:** [November 11, 2024, 10:49pm UTC](https://forums.swift.org/t/value-wrapped-in-a-thunk-recursively/75925 "2024-11-11T22:49:52Z")

</div>

Hello guys! I'm trying to understand why the code below works not quite as I expect it to work. What i've tried to achieve is to store weak references to objects with the help of closures. In real world these were dele…

---

## [What is repeat { $0 + 1 }(each t)](https://forums.swift.org/t/what-is-repeat-0-1-each-t/75545)

<div class="topic-metadata">

**Author:** [@ibex](https://forums.swift.org/u/ibex)\
**Replies:** 2\
**Last updated:** [October 23, 2024, 12:25am UTC](https://forums.swift.org/t/what-is-repeat-0-1-each-t/75545 "2024-10-23T00:25:06Z")

</div>

I was reading this post by @vry and encountered this cute expression: repeat { $0 + 1 }(each t) Could anyone explain what this expression does for the benefit of the learners? I could make an attempt, but I don't fee…

---

## [A need for a once-callable closure type](https://forums.swift.org/t/a-need-for-a-once-callable-closure-type/74485)

<div class="topic-metadata">

**Author:** [@OneSadCookie](https://forums.swift.org/u/OneSadCookie)\
**Replies:** 12\
**Last updated:** [September 12, 2024, 9:57am UTC](https://forums.swift.org/t/a-need-for-a-once-callable-closure-type/74485 "2024-09-12T09:57:11Z")

</div>

Writing Swift 6, I've hit this problem a few times now: func callOnce(\_ closure: () -\> Void) { closure() } public struct NotSendable {} func send(\_ value: sending NotSendable) { // not relevant } func example…

---

## [Cannot infer type returned by previous closure](https://forums.swift.org/t/cannot-infer-type-returned-by-previous-closure/74218)

<div class="topic-metadata">

**Author:** [@antoniopantaleo](https://forums.swift.org/u/antoniopantaleo)\
**Replies:** 2\
**Last updated:** [August 28, 2024, 5:10pm UTC](https://forums.swift.org/t/cannot-infer-type-returned-by-previous-closure/74218 "2024-08-28T17:10:30Z")

</div>

I wrote a simple function I wanted to use in my tests, to separate test phases into closures: func given\<T\>( \_ setup: () -\> T, when: (inout T) -\> Void = { \_ in }, then: (T) -\> Void ) { var sut = setup() …

---

## [How does a closure capture local variables by reference?](https://forums.swift.org/t/how-does-a-closure-capture-local-variables-by-reference/74080)

<div class="topic-metadata">

**Author:** [@ibex](https://forums.swift.org/u/ibex)\
**Replies:** 1\
**Last updated:** [August 21, 2024, 2:56am UTC](https://forums.swift.org/t/how-does-a-closure-capture-local-variables-by-reference/74080 "2024-08-21T02:56:09Z")

</div>

After reading this post, I have felt that the following question is not adequately exaplained in TSPL. How does a closure capture local variables by reference? TSPL provides an excellent example but doesn't really exp…

---

## [Dears, the Closures chapter make me Drunk!](https://forums.swift.org/t/dears-the-closures-chapter-make-me-drunk/73831)

<div class="topic-metadata">

**Author:** [@mgstanley](https://forums.swift.org/u/mgstanley)\
**Replies:** 1\
**Last updated:** [August 8, 2024, 2:25pm UTC](https://forums.swift.org/t/dears-the-closures-chapter-make-me-drunk/73831 "2024-08-08T14:25:06Z")

</div>

That chapter tells Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios. As programming beginners, I really do not think so. Operator M…

---

## [Overloading on the isolation of a closure parameter](https://forums.swift.org/t/overloading-on-the-isolation-of-a-closure-parameter/72476)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 5\
**Last updated:** [June 17, 2024, 1:25pm UTC](https://forums.swift.org/t/overloading-on-the-isolation-of-a-closure-parameter/72476 "2024-06-17T13:25:10Z")

</div>

Here's the code that I perhaps naively attempted to write: public func expectError( from closure: ()async throws-\>() ) async throws { do { try await closure() throw "ExpectedErrorButGotNone"…

---

## [A combination of defer-statement and closure may cause crash](https://forums.swift.org/t/a-combination-of-defer-statement-and-closure-may-cause-crash/71692)

<div class="topic-metadata">

**Author:** [@xrloong](https://forums.swift.org/u/xrloong)\
**Replies:** 0\
**Last updated:** [May 8, 2024, 10:21am UTC](https://forums.swift.org/t/a-combination-of-defer-statement-and-closure-may-cause-crash/71692 "2024-05-08T10:21:25Z")

</div>

Here is the code causing crash: import Foundation let KEY = "key" func doHeavyWork() { print("Enter doHeavyWork") defer { print("Leave doHeavyWork") } defer { let callback: () -\> () = { …

---

## [Force new Xcode multiplatform projects to default to a specific device and target](https://forums.swift.org/t/force-new-xcode-multiplatform-projects-to-default-to-a-specific-device-and-target/71619)

<div class="topic-metadata">

**Author:** [@bcardarella](https://forums.swift.org/u/bcardarella)\
**Replies:** 0\
**Last updated:** [May 4, 2024, 8:01pm UTC](https://forums.swift.org/t/force-new-xcode-multiplatform-projects-to-default-to-a-specific-device-and-target/71619 "2024-05-04T20:01:37Z")

</div>

I'm using Xcodegen (yes I've opened an issue on that repo already three weeks ago, no replies) to generate multiplatform projects. Is there a way for Xcode projects newly generated to set the default device and target? …

---

## [Constrain type to "representible in C" for use with @convention(c) and generics?](https://forums.swift.org/t/constrain-type-to-representible-in-c-for-use-with-convention-c-and-generics/71076)

<div class="topic-metadata">

**Author:** [@natinusala](https://forums.swift.org/u/natinusala)\
**Replies:** 2\
**Last updated:** [April 4, 2024, 4:00pm UTC](https://forums.swift.org/t/constrain-type-to-representible-in-c-for-use-with-convention-c-and-generics/71076 "2024-04-04T16:00:41Z")

</div>

The following code: func test\<each Input, Output\>(\_ closure: @convention(c) (repeat each Input) -\> Output) {} Fails with the following error: error: '(repeat each Input) -\> Output' is not representable in Objective-C,…

[Next page](https://forums.swift.org/tag/closure/195.md?match_all_tags=true&page=1&tags%5B%5D=closure)
