Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between function and closure assignment

Is there any difference in swift between function declaration:

func function(a: String) {
    print(a);
}
function("test");

and closure assignment:

let closure = {
    (a: String) in
    print(a);
}
closure("test");

Is there any difference between those?

like image 899
Krzysztof Kaczor Avatar asked Nov 14 '14 08:11

Krzysztof Kaczor


People also ask

Is a function a closure?

The term closure is often used as a synonym for anonymous function, though strictly, an anonymous function is a function literal without a name, while a closure is an instance of a function, a value, whose non-local variables have been bound either to values or to storage locations (depending on the language; see the ...

Is a closure the same as an anonymous function?

An anonymous function is just a function that has no name; nothing more. A closure is a function that captures the state of the surrounding environment. An anonymous function does not necessarily need to create a closure, and a closure is not created only for anonymous functions.

What is closure in functional programming?

A closure is a programming technique that allows variables outside of the scope of a function to be accessed. Usually, a closure is created when a function is defined in another function, allowing the inner function to access variables in the outer one.

Why is it called function closure?

A closure is a function and its scope assigned to (or used as) a variable. Thus, the name closure: the scope and the function is enclosed and used just like any other entity.


1 Answers

  1. Named or anonymous

    func function(a: String) {
        print("\(a), name: \(__FUNCTION__)");
    }
    
    let closure = { (a: String) in
        print("\(a), name: \(__FUNCTION__)");
    }
    
  2. Capture List
    supported in closures only:

    let obj = FooClass()
    let closure = { [weak obj] in ... }
    
  3. Curried function
    supported in functions only:

    func curriedFunc(x:Int)(y:Int) { ... }
    let curried = curriedFunc(1)
    curried(y:2)
    

    Similar, but not exact the same with using closure:

    let closure = { (x:Int) in { (y:Int) in ... }}
    
  4. Generics
    supported in functions only:

    func function<T>(x:T) { ... }
    
  5. Referenceability from its own initial declaration
    supported in global functions only:

    func recursive(var x:Int) -> Int {
        ...
        return condition ?  x : recursive(x)
    }
    

    You can do this using closure also:

    var recursive:((Int) -> Int)!
    recursive = { (var x) in
        ...
        return condition ? x : recursive(x)
    }
    

    But this is not recommended because this causes strong reference cycles.

  6. Overload
    supported in functions only:

    func function(a: String) { print("String: \(a)") }
    func function(a: Float) { print("Float: \(a)") }
    

    n.b. You can reference them as a closure like this:

    let f:(Float) -> Void = function
    
like image 149
rintaro Avatar answered Nov 15 '22 07:11

rintaro