

Swift: Avoid auto return in closures
source link: https://dcordero.me/posts/swift_avoid_auto_return_in_closures.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

David Cordero
Swift: Avoid auto return in closures
Published on 06 Dec 2014
In Swift, you have several options for writing closures more concisely.
When a closure’s type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both. Single statement closures implicitly return the value of their only statement.
Because of this feature, We can create super neat closures like the following one:
let sortedNumbers = sorted(numbers) { $0 > $1 }
But sometimes, when we are not looking for a functional programming solution, it is a pity to deal with this implicit return.
For example when we only want to call a single function from our closure:
func someFunctionThatReturnSomething () -> Bool {
return true
}
func someFunctionThatTakesAClosure ( closure: Void -> Void) {}
// As it has a single line it FAILS because it tries to return the result of someFunctionThatReturnSomething, which is not Void
someFunctionThatTakesAClosure( {
someFunctionThatReturnSomething() // Compilation error
})
To deal with this issue we have a couple options:
Solution 1: Add an explicit return
someFunctionThatTakesAClosure( {
someFunctionThatReturnSomething()
return
})
Solution 2: Ignore the result from someFunctionThatReturnSomething
someFunctionThatTakesAClosure( {
_ = someFunctionThatReturnSomething()
})
Although I honestly don’t like any of both solutions, I prefer the second solution since it keeps the concise style of Swift and makes it clear that you are purposefully and knowingly ignoring the return value from the function.
This implicit return is definitely creating more pain than goodness.
⚠️Update: Swift 1.2
This is not longer an issue with the new version 1.2 of Swift. As it is described in the releases notes:
Unannotated single-expression closures with non-Void return types can now be used in Void contexts.
Recommend
-
31
To help write robust, reliable, and easy-to-test software, I always recommend purifying your code of effects. There are a bunch of tricks and techniques to accomplish this sort of thing, and I’m going to share one of my f...
-
10
Understanding Opaque Return Types in Swift Understanding Opaque Return Types in Swift June 10, 2019 Why are SwiftUI's return types some View?Why can't it just return...
-
10
Recipe for Calling Swift Closures from Asynchronous Rust Code The purpose of this recipe is to integrate asynchronous Rust with idiomatic Swift code that uses completion handler blocks. All reference counting s...
-
12
05 Apr 2021 Updated: 06 Apr 2021 software-dev ...
-
12
10 Jun 2018 software-dev
-
4
Copy link Contributor FabianWolff commented
-
7
What is @escaping in Swift closures 19 Oct 2020 ⋅ 5 min read ⋅ Swift
-
9
Why should we avoid using closures in Swift structs? Jan 10, 2020 • Rizwan •...
-
8
Why Retailers Are Charging Return Fees and How to Avoid Them US Markets Loading... In the news...
-
8
[let_and_return]: avoid linting when code between last stmt and return expr is cfg'd out #12558 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK