Adding computed properties to a base type with extensions

Swift allows us to add both computed instance properties and computed type properties to an existing type. These are the only types of properties that we can add to an existing type, so we cannot add simpler stored properties using extensions.

When you need to perform calculations with values that have an associated unit of measurement, it is very common to make mistakes by mixing its different units. It is also common to perform incorrect conversions between the different units that generate wrong results. Swift doesn't allow us to associate a specific numerical value with a unit of measurement. However, we can add computed properties to provide some information about the units of measurement for a specific domain.

Tip

We worked with units when we analyzed the object-oriented approach of the HealthKit framework in Chapter 1, Objects from the Real-Word to the Playground. However, in this case, we just want to simplify a sum operation with a specific resistance unit.

The need to associate quantities with units of measurement in any programming language is easy to understand even in the most basic math and physics problems. One of the simplest calculations is to sum two values that have an associated base unit. For example, say that you have two electrical resistance values. One of the values is measured in ohms and the other in kilo-ohms. To sum the values, you must choose the desired unit and convert one of the values to the chosen unit. If you want the result to be expressed in ohms, you must convert the value in kilo-ohms to ohms, sum the two values expressed in ohms, and provide the result in ohms.

The following code uses variables with a suffix that defines the specific unit being used in each case. You have probably used or seen similar conventions. The suffixes make the code less error prone because you easily understand that r1InOhms holds a value in ohms, and r2InKohms holds a value in kilo-ohms. Thus, there is a line that assigns the result of converting the r2InKohms value to ohms to the new r2InOhms variable. The last line calculates the sum and holds the result in ohms because both variables hold values in the same unit of measurement. The code file for the sample is included in the swift_3_oop_chapter_08_03 folder.

    var r1InOhms = 500.0 
    var r2InKohms = 5.2 
    var r2InOhms = r2InKohms * 1e3 
    var r1PlusR2InOhms = r1InOhms + r2InOhms 

Obviously, the code is still error prone because there won't be any exception thrown or syntax error if a developer adds the following line to sum ohms and kilo-ohms without performing the necessary conversions. The code file for the sample is included in the swift_3_oop_chapter_08_04 folder.

    // The following line produces a wrong result 
    var r3InOhms = r1InOhms + r2InKohms 

There is no rule that ensures that all the variables included in the sum operation must use the same suffix, that is, the same unit. There aren't invalid operations between variables that hold values with incompatible units. For example, you might sum a voltage value to a resistance value, and the code won't produce any error or warning.

The following lines use the extension keyword to add three get-only computed properties to the Double standard type: ohm, kohm, and mohm. The code file for the sample is included in the swift_3_oop_chapter_08_05 folder.

    public extension Double { 
      public var ohm: Double { return self } 
      public var kohm: Double { return self * 1e3 } 
      public var mohm: Double { return self * 1e6 } 
    } 

The ohm get-only computed property returns self, that is, the actual value for Double. The kohm get-only computed property returns self multiplied by 1,000. In this case, the code uses the exponential notation, where 1e3 means 10 to the third power, that is, 10 * 10 * 10. Finally, the mohm get-only computed property returns self multiplied by 1,000,000. In this case, the code uses the exponential notation where 1e6 means 10 to the sixth power, that is, 10 * 10 * 10 * 10 * 10 * 10.

After we add the previous extensions, we want to perform the following calculation: 500 ohms + 5.2 KOhms + 3.1 MOhms. If we convert all the values to ohms and express the result in ohms, we must calculate 500 ohms + 5,200 ohms + 3,100,000 ohms. We can declare three variables with the number followed by a dot and the extension we created to convert the number to the value in a baseline ohm unit. The extension methods will return a Double number that will be always converted to ohms. Then, we can easily calculate the total resistance value in ohms by computing the sum of the three variables. The following lines declare three variables, and each one uses the get-only computed property to specify the specific unit in which the original value is expressed: ohm, kohm, or mhom. Then, the code prints the real values stored in the three variables: resistance1, resistance2, and resistance3. The three values are stored in ohms because the get-only computed property returns the result of the conversion of each unit to ohms. Then, the code computes the sum of the three variables and stores the result expressed in ohms in the totalResistance variable. The code file for the sample is included in the swift_3_oop_chapter_08_05 folder.

    var resistance1 = 500.0.ohm 
    var resistance2 = 5.2.kohm 
    var resistance3 = 3.1.mohm 
    print("resistance1 in ohms: (resistance1)") 
    print("resistance2 in ohms: (resistance2)") 
    print("resistance3 in ohms: (resistance3)") 
   
    var totalResistance = resistance1 + resistance2 + resistance3 
 
    print("Total resistance in ohms: (totalResistance)") 

The following lines show the output generated after executing the preceding code. The code file for the sample is included in the swift_3_oop_chapter_08_05 folder.

resistance1 in ohms: 500.0
resistance2 in ohms: 5200.0
resistance3 in ohms: 3100000.0
Total resistance in ohms: 3105700.0

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

Adding computed properties to a base type with extensions

We can take advantage of Swift's flexibility with property names and use the Greek omega letter (Ω) instead of the ohm word in each of the get-only computed properties. You can easily insert the Greek omega letter in Mac OS by pressing Alt + Z . You can check other symbols in Mac OS by pressing Command + Control + Space . The following lines use the extension keyword again to add three get-only computed properties to the Double standard type: Ω, , and . The code file for the sample is included in the swift_3_oop_chapter_08_06 folder.

    public extension Double { 
      public var Ω: Double { return self } 
      public var KΩ: Double { return self * 1e3 } 
      public var MΩ: Double { return self * 1e6 } 
    } 

The following lines declare three variables, and each one uses the get-only computed property to specify the specific unit in which the original value is expressed: Ω, , or . Then, the code prints the real values stored in the three variables---resistance4, resistance5, and resistance6---then it computes the sum and prints the result. The code looks really nice because it is easy to understand the unit in which each resistance value is expressed. The code file for the sample is included in the swift_3_oop_chapter_08_06 folder.

    var resistance4 = 500.0.Ω 
    var resistance5 = 5.2.KΩ 
    var resistance6 = 3.1.MΩ 
    print("resistance4 in Ω: (resistance4)") 
    print("resistance5 in Ω: (resistance5)") 
    print("resistance6 in Ω: (resistance6)") 
 
    var totalResistance456 = resistance4 + resistance5 + resistance6 
 
    print("Total resistance in Ω: (totalResistance456)") 

The following lines show the output generated after executing the preceding code:

resistance4 in Ω: 500.0
resistance5 in Ω: 5200.0
resistance6 in Ω: 3100000.0
Total resistance in Ω: 3105700.0

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

Adding computed properties to a base type with extensions

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

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