Is there a difference between a quick function declaration:
func function(a: String) { print(a); } function("test");
and circuit purpose:
let closure = { (a: String) in print(a); } closure("test");
Is there a difference between the two?
Named or Anonymous
func function(a: String) { print("\(a), name: \(__FUNCTION__)"); } let closure = { (a: String) in print("\(a), name: \(__FUNCTION__)"); }
Capture Listsupported only in closure:
let obj = FooClass() let closure = { [weak obj] in ... }
Curried functiononly supported in features:
func curriedFunc(x:Int)(y:Int) { ... } let curried = curriedFunc(1) curried(y:2)
Similarly, but not the same using closure:
let closure = { (x:Int) in { (y:Int) in ... }}
Genericsonly supported in features:
func function<T>(x:T) { ... }
Ordering from your own original declarationonly supported in global :
func recursive(var x:Int) -> Int { ... return condition ? x : recursive(x) }
You can also do this with a closure:
var recursive:((Int) -> Int)! recursive = { (var x) in ... return condition ? x : recursive(x) }
But this is not recommended because it causes strong reference loops.
overloadonly supported in features:
func function(a: String) { print("String: \(a)") } func function(a: Float) { print("Float: \(a)") }
nb You can refer to them as closing as follows:
let f:(Float) -> Void = function
Another difference: recursion inside another function
Nested functions cannot be recursive:
func foo() { func bar() { bar() } // does not compile }
but closures inside other functions can be recursive:
func foo() { var bar: (() -> ())! bar = { bar() } }
Source: https://habr.com/ru/post/978180/More articles:How to do a numerical search using Material Design - androidCustom search bar with material design - androidDjango: how to override CSRF_FAILURE_TEMPLATE - pythonDocument pages and phpdocumentor templates - phphttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/978179/azure-web-role-with-transient-fault-handling-block-exception-the-path-is-too-long-after-being-fully-qualified&usg=ALkJrhjTUU7jasOhRT2qte4GSZo25npAzQphpDocumentor page level docBlock in html file included - phpdocHow can I output odut 8 datetime field without time in qweb report? - xmlGoogle GCM - Do not receive push notifications in android Lollipop - androidAn instance of a poorly performing non-tail recursive function - algorithmHTML5 srcset: mixing x and w syntax - html5All Articles