Swift: definition and syntax of functions against closing

Is this a function or a closure?

let triple: Int -> Int = { (number: Int) in // What is this? let result = 3 * number number return result } triple(1) 
+7
function closures swift
source share
5 answers

1: This is a short circuit.

Examples:

 func aFunction() -> () { // do something } let aClosure:() -> () = { // do something } 

Functions are actually a special case of closures. You can write an unnamed closure with surrounding code in parentheses ({}). To use in separate arguments and return type from body.

Excerpt from: Apple Inc. "Fast programming language." interactive books. https://itun.es/us/jEUH0.l

2: "in" is only the designation chosen to represent where the arguments / return type ends and the closure body begins. This has nothing to do with inout.

+9
source share

This is the binding of the closure to the triple constant, which, if executed in the global area, is similar to declaring it as a function (see Eric's comment below for differences). The string (number: Int) in declares the input parameters to close.

In this case, the type can be omitted because, since it can be inferred from the closure type, and since it has only one statement, it can be written as an closure expression with implicit return as:

 let triple: Int -> Int = { number in 3 * number } // or as a function func triple(number: Int) -> Int { return 3 * number } // Or as the very short let triple: Int -> Int = { 3 * $0 } 

You can read more about this in the official chapter on closing documentation.

https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html

+5
source share

Swift Closures are defined as follows:

 { (parameters) -> return type in statements } 

Thus, your sample code is considered closure by definition. However, I would rewrite it as follows, moving the return type into curly braces:

 let triple = { (number: Int) -> Int in let result = 3 * number number return result } triple(1) 

Parameters and the type of the return value begin after opening the curly bracket and end before the keyword, and the body of the closure begins after the keyword in and ends in the closing curly bracket. Further, I would recommend downloading and reading the iBook from iTunes, which can be found at the following address:

https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

+4
source share

A simple way to distinguish functions and closures is to

Global function: specify a name . Do not record any values.

Nested function: specify a name . Capturing values ​​from a closing function.

Closure: Does not have a name as a function. Capturing values ​​from neighboring blocks.

+3
source share

A function is a closure with a name. The closure you provided has a name and therefore is a function and closure.

From Apple Developer Website:

The global and nested functions introduced in Functions are actually special cases of closure. Closures take one of three forms:

Global functions are closures that have a name and do not commit values. Nested functions are locks that have a name and can capture values ​​from their closing function. Closing expressions are not labeled closures written in lightweight syntax that can capture values ​​from their surrounding context.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

+3
source share

All Articles