Since a function or closure can be assigned to a variable or constant, it must have a type. Closure types are first-class types in Swift. Discover how their type definition is made up of the types of the parameters for the closure/function and the return type so a variable/constant can be assigned and set to anything of that type.
- [Instructor] So we've been talking about closures…and functions and how functions are closures with names.…Well also, it's important to understand…that closures and functions have types.…In fact, there are first class types in Swift.…So they also can be types for parameters in functions.…Well, what do you do with parameters in functions?…You pass them in, so they can be passed around.…Again, going back to the definition of a closure;…it can be passed around.…So our examples before had a type,…they took two Ints and returned an Int…and it looks just like this:…Int-Int-returns-Int.…
At least that's how I say it.…What do I know?…I used to say ASCII was a a-s-c-two,…I thought it was Roman numerals.…I read everything, so that's usually how I pronounce it.…So I would say the type of those closures…were Int-Int-return-Int.…So this function has a type,…it takes two parameters a String…and an Int returns a String.…So I would say that type is String-Int-returns-String,…that's its type and just like these parameters have a type…
- What are closures?
- Closures and function types
- Basic higher-order functions, including sort and forEach
- Powerful higher-order functions, including map and reduce
- Passing functions
- Closure optimizations
- Higher-order functions on sets, dictionaries, and strings