Reputation: 33
This gives me an error in the playground.
func returnAFunc() -> ()
{
func f(){ println("hello") }
return f
}
Now I read that as a func named "returnAFunc" which returns another func which doesn't return a value. Correct? But it doesn't work. I have to do this:
func returnAFunc() -> () -> () // or this (() -> ())
{
func f(){ println("hello") }
return f
}
Ok that doesn't seem right. Can someone explain?
Upvotes: 3
Views: 808
Reputation: 237010
The type signature of your first returnAFunc
is () -> ()
, which means a function that takes no arguments and returns nothing. The type for your second function is () -> () -> ()
, which means a function that takes no arguments and returns a function with type () -> ()
(i.e. a function that takes no arguments and returns nothing).
I think your basic misunderstanding is that ()
represents a function type. Instead, it's the same thing as Void. Once you understand this, the only other potential "gotcha" is to remember that the arrow is right-associative.
EDIT: Since I got the checkmark, I think it's worth calling attention to IMSoP's answer, which does a great job of explaining how to read the type in more detail.
Upvotes: 4
Reputation: 89162
()
is an empty tuple. Swift uses it as the return type of a void function, so
() -> ()
Is the signature of a function that takes no parameters and returns nothing (is void)
So,
func myFunc() -> ()
declares a function that returns nothing (is void), and
func myFunc() -> () -> ()
declares a function that returns a function, which takes no arguments and returns nothing.
Upvotes: 3
Reputation: 97688
Lets break down your definition:
func returnAFunc() -> ()
func
keywordreturnAFunc
function name()
parameters expected (void/none)->
read as "returns"()
void return typeSo this function takes no parameters, and returns nothing.
The working version breaks down like this:
func returnAFunc() -> () -> ()
func
keywordreturnAFunc
function name()
parameters expected (void/none)->
read as "returns"() -> ()
return type is another function specification:
()
returned function takes no parameters->
read as "returns"()
returned function returns voidSo this function takes no parameters, and returns (a function which takes no parameters and returns nothing). (Eesh, even in English I feel the need to add extra parentheses to that...)
Upvotes: 6
Reputation: 9262
Your first example reads as "Function returnAFunc
takes no arguments, and returns no values." Your second example correctly defines the return type as "Function returnAFunc
takes no arguments, and returns a function that takes no arguments and returns no arguments."
The syntax is a bit wonky, to be sure, but once you get used to it, it's pretty straightforward to see what's going on.
In addition, the Swift type void
is represented as "()
", so you could also interpret this as "Function returnAFunc
takes a void argument (which means no argument) and returns a void type (which means no argument)." I'm not sure that's actually what's happening behind the scenes, though.
Upvotes: 1