简体   繁体   中英

A func returning a func in Swift

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?

Lets break down your definition:

func returnAFunc() -> ()
  • func keyword
  • returnAFunc function name
  • () parameters expected (void/none)
  • -> read as "returns"
  • () void return type

So this function takes no parameters, and returns nothing.

The working version breaks down like this:

func returnAFunc() -> () -> ()
  • func keyword
  • returnAFunc 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 void

So 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...)

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 () -> () (ie 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.

() 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM