Namespaces for functions and variables in Swift

If you run this code, the variable f seems to obscure the function f. In any case, in order to achieve the function f?

func f (a:Int)->Int{ return a + 43 } var f = {(a:Int) in a + 42} var z = f(1) println(z) 
+5
source share
1 answer

Not.

In Swift, function declarations are just shortcuts to what you did with this closure + variable. That is, function names are essentially constants and should always be treated as such (you can even pass a function name without parentheses as a reference).

What you are doing is reusing the name f to close the variable. Swift seems to have a compiler problem that doesn't complain about it. However, this problem will never occur in good code, so this is not a real problem.

This can be a bit confusing.

+1
source

Source: https://habr.com/ru/post/1201853/


All Articles