Discussion about this post

User's avatar
David Peterson's avatar

Good post! I would just add that for me, `@autoclosure` is best reserved for situations where you have a parameter value that a) could be expensive and b) may never need to be executed.

The nil-coalescing example is a good one, in that if the optional value you're coalescing is not nil, the remainder of the expression never need to execute.

Another classic example is `assert`. For example:

```

assert(someValue == someOtherValue, "Expected \(someOtherValue) but got \(someValue).")

```

Both the condition being tested and the message have a processing cost. If we're not running in debug mode, assertions always pass, so processing them would be less efficient. And the message will only be output when the assertion fails.

So, this is where autoclosure is useful - it makes it simple to wrap up code that may not need to execute.

Expand full comment
Tomasz Lizer's avatar

Very nice post!

It clearly shows biggest fail of Swift 6 introduction which was not adhering to its "progressive disclosure" principle which makes Swift so nice (and that release so painful to developers that tried migrating).

Thankfully most of that got fixed with Approachable Concurrency and Swift Default Actor isolation.

Nevertheless as strange as those concurrency annotation might be looking I think those are great as this gives very verbose overview of the code execution. I have seen too many random threading issues due to sloppy usage of callbacks and those things are here to help :)

Also a tip: when specifying callback to be @MainActor isolated @Sendable becomes redundant as we already specified thread boundary crossing with it :)

Expand full comment

No posts