Generic Functions and Methods

I’m a little unsure about Figure 22.1 myMap declaration. What does this closure syntax (T) -> (U) mean in the declaration of the second parameter to myMap? Is it a closure taking a single parameter of type T and returning a function taking a single parameter of type U?

You nearly got it right :slight_smile:

(T)->(U) denotes the type or (signature) of a function which takes a parameter of type T and returns a value of type U. Also, (T)->U means the same.

func foo (int:Int) -> String {
    return "foo: \(int)"
}

let bar : (Int) -> (String) = foo
print ("\(bar (1))")

let jibber : (Int) -> String = foo
print ("\(jibber (1))")

1 Like

Thanks, that’s cleared that up for me :slight_smile: