Working with function types within classes

The following lines declare a myFunction variable with a function type, specifically, a function that receives an Int argument and returns a Bool value. The variable works in the same way as an argument that specifies a function type for a function. The code file for the sample is included in the swift_3_oop_chapter_07_05 folder:

    var myFunction: ((Int) -> Bool) 
    myFunction = divisibleBy5 
    let myNumber = 20 
    print("Is (myNumber) divisible by 5: (myFunction(myNumber))") 

Then, the code assigns the divisibleBy5 function to myFunction. It is very important to understand that the line doesn't call the divisibleBy5 function and save the result of this call in the myFunction variable. Instead, it just assigns the function to the variable that has a function type. The lack of parenthesis after the function name makes the difference.

Then, the code prints whether the Int value specified in the myNumber constant is divisible by 5 or not using the myFunction variable to call the referenced function with myNumber as an argument.

The following screenshot shows the results of executing the previous lines in the Playground. Note that the result of executing myFunction = divisibleBy5 displays an Int -> Bool type on the right-hand side:

Working with function types within classes

Type inference also works with functions, so we might replace the two lines that declared the myFunction variable and assigned the divisibleBy5 function with the following single line. The code file for the sample is included in the swift_3_oop_chapter_07_06 folder:

    var myFunction = divisibleBy5 

So far, we have worked with function types in functions. We can definitely take advantage of function types in object-oriented code. For example, the following lines show the code for a new NumberWorker class that declares the appliedFunction method with a function type as a parameter type. The code file for the sample is included in the swift_3_oop_chapter_07_06 folder:

    open class NumbersWorker { 
      private var numbers = [Int]() 
     
      init(numbers: [Int]) { 
        self.numbers = numbers 
      } 
    open func appliedFunction(condition: (Int) -> Bool) -> [Int] { 
        var returnNumbers = [Int]() 
        for number in numbers { 
          if condition(number) { 
            returnNumbers.append(number) 
          } 
        } 
         
        return returnNumbers 
      } 
    }  

The following lines show the code for the NumberFunctions class that defines the isDivisibleBy5 type method. We will use this type method as an argument when we'll call the appliedFunction method that we coded in the NumbersWorker class. The code file for the sample is included in the swift_3_oop_chapter_07_07 folder:

    open class NumberFunctions { 
      open static func isDivisibleBy5(number: Int) -> Bool { 
        return number % 5 == 0 
      } 
    }

The following lines create a numbersList array of Int and then pass it as an argument to the initializer of the NumbersWorker class. The last line calls the worker.appliedFunction method with the NumberFunctions.isDivisibleBy5 type method as an argument. The code file for the sample is included in the swift_3_oop_chapter_07_07 folder:

    var numbersList = [-60, -59, -48, -35, -25, -10, 11, 12, 13, 14, 
                      15] 
    var worker = NumbersWorker(numbers: numbersList) 
    var divisibleBy5List = worker.appliedFunction
    (condition: NumberFunctions.isDivisibleBy5) 
    print(divisibleBy5List)

In this case, we used a type method as the argument for a method that specified a function type as a parameter type. We can also use an instance method as an argument that requires a function type.

The following screenshot shows the result of executing the previous lines in the Playground:

Working with function types within classes

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.46.109