Intro to Closures Part 1

Introduction

Welcome to Lesson of 1 of Intro to Functional Swift. You will learn what makes closures different from the Swift functions. If you’ve never used closures before, this lesson will be tough.

Problem

Who are you closures, () -> ()?

Definition

  • A closure is a function without name and the func keyword (for now).
  • Quick and easy to carry around as a variable/constant or pass as a parameter

Functions

To appreciate closures, let us begin with functions. Create a function that add two numbers.

  1. func addTwoNumbers(number1: Int, number2: Int) -> Int {
  2. return number1 + number2
  3. }

First Class Citizen

You may store a function into a variable/constant.

  1. var addFunction = addTwoNumbers
  2. addFunction(10, 30) // 40

Introducing Closures

Create a closure whose functionality is identical as above. A closure block begins and ends with {,}.

  1. var addClosures: (Int, Int) -> Int = {
  2. (number1: Int, number2: Int) in
  3. return number1 + number2
  4. }
  5. addClosures(4, 10) // 14

The type of addClosures is (Int, Int) -> Int. It is identical to addTwoNumbers from the early example. number1 and number2 are the parameter labels. Anything comes after the keyword,in, is a return block.

Short Form

It is not required to enter return after in only if the closure solely returns value.

  1. var addClosuresTwo = {
  2. (number1: Int, number2: Int) in
  3. print("Hello")
  4. return number1 + number2 // return required
  5. }
  6. var addClosuresThree = {
  7. (number1: Int, number2: Int) in
  8. number1 + number2 // return not required
  9. }
  10. addClosuresThree(4, 6)

The addClosuresTwo requires return since there is an additional line of print("Hello"). Besides, you may skip the return keyword.

Shortest Form

As long as you specify the type, you may not create an argument label.

  1. var addClosuresFour: (Int, Int) -> Int = { $0 + $1 }
  2. addClosuresFour(4, 5) // 9

In the example above, $0 and $1 refer to the first and second parameter. Swift recognizes 4 as $0 and 5, $1 from addClosuresFour(4, 5).

Another Example

Let us create a function that does not require any parameter but returns String.

  1. func callString() -> String {
  2. return "hello, I'm a function"
  3. }

Let us express the callString function with a closure block instead.

  1. let callStringWtihClosure: () -> String = { () in
  2. print("hello")
  3. return "hello, I'm a closure"
  4. }
  5. callStringWtihClosure()
  6. // "hello, I'm a closure"
  7. // Skip Return
  8. let callStringWtihClosureTwo: () -> String = { () in
  9. "hello, I'm a closure"
  10. }

Short Form {#no-parameter}

If you pass no parameter, you may ignore explicit type definition.

  1. // Type defined explicitly
  2. let callStringWtihClosureThree: () -> String = { "hello, I'm a closure" }
  3. // Type defined implicitly
  4. let callStringWtihClosureFour = { "hello, I'm a closure" }
  5. callStringWtihClosureFour // () -> String

Resource

No Fear Closure in Swift 3 with Bob

Source Code

3001_intro_closures_part1.playground

Conclusion

To recap, you’ve learned functions and closures are identical. However, you may express closures using various forms which often lead to zenness and effective communication. You’ve learned the basics. You will discover the power of closures as you step up one at a time.

You may feel uncomfortable. You should be. In fact, if you are not struggling, you are not learning. Accumulating knowledge goes against the Second Law of Thermodynamics that the universe wants to remain scattered. When you defy the law as a human being, you face consequences. Embrace the pain and it’s a natural phenomenon.

In the following lesson, you will how learn how to pass and return a closure block within a function.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.