Generic function parameters
So far, you’ve looked at definitions of generic types in functions, classes, structures, and enumerations. With the exception of Dictionary all of these had a single generic parameter.
The generic type parameter list comes after the type name or function name. You can then use the generic parameters in the rest of the definition. This function takes two arguments and swaps their order:
func swapped<T, U>( x: T, y: U) -> (U, T) { return (y, x)
}
swapped(33, "Jay") // returns ("Jay", 33)
A generic function definition demonstrates a confusing aspect about the syntax: having both type parameters and function parameters. You have both the generic parameter list of type parameters <T, U>, and the list of function parameters ( x: T, y: U).
Think of the type parameters as arguments for the compiler, which it uses to define one possible function. Just as your generic Keeper type meant the compiler could make dog keepers and cat keepers and any other kind of keeper, the compiler can now make a non-generic specialized swapped function for any two types for you to use.