Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces for functions and variables in Swift

Tags:

swift

If you run this code, the variable f seems to shadow the function f. Is there anyway to reach the function f?

func f (a:Int)->Int{
   return a + 43
}

var f = {(a:Int) in a + 42}

var z = f(1)
println(z)
like image 909
cfischer Avatar asked Nov 11 '22 02:11

cfischer


1 Answers

No.

In Swift, function declarations are simply shortcuts for what you did with that closure + variable thing. That is, function names are essentially constants and should always be viewed as such (you can even pass around the function name, without brackets, as a reference).

What you're doing is you're redeclaring the name f to the variable closure. It seems Swift has a compiler issue not complaining about this. However, this problem would never occur in good code, so it's not a real problem.

It can be a bit confusing, though.

like image 93
ArVID220u Avatar answered Dec 06 '22 23:12

ArVID220u