An Extension Function
is a function that is attached to an existing class and are invoked like member functions. For this post, we are using Kotlin 1.1
.
Member Functions
In OOP
, we have a class and it has member properties and behaviors. A member function is a class’ behavior.
[wp_ad_camp_1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.turreta.kotlin.extension class Automobile { var make: String? = null var year: Int? = null fun startEngine() { println("Behavior to start the engine") } fun pressBreak() { println("Behavior to stop") } fun pressAccelerator() { println("Behavior to accelerate") } } |
Extension Function
Given the class declaration above, we would create an Extension Function this way:
1 2 3 4 5 6 7 8 9 10 | fun Automobile.startHazardBlinkers() { println("Car ${this.make} [${this.year}] - Hazard blinkers are now on!") println("*** Pressing emergency break no!") this.pressBreak() } fun Automobile.stoptHazardBlinkers() { println("Car ${this.make} [${this.year}] - Hazard blinkers are now off!") } |
Note is this example that we are referencing the instance variables make and year. We also invoke a member function from our extension function.
[wp_ad_camp_2]
Sample Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 | fun main(args: Array<String>) { val toyota: Automobile = Automobile() toyota.make = "Toyota Vios" toyota.year = 2016 toyota.startEngine() toyota.pressAccelerator() toyota.pressBreak() toyota.startHazardBlinkers() toyota.stoptHazardBlinkers() } |
Output
[wp_ad_camp_3]
1 2 3 4 5 6 7 | Behavior to start the engine Behavior to accelerate Behavior to stop Car Toyota Vios [2016] - Hazard blinkers are now on! *** Pressing emergency break no! Behavior to stop Car Toyota Vios [2016] - Hazard blinkers are now off! |
Extensions are resolved statically
This means that the “attached” function is only available from instances of the specific class the function was attached to. We cannot use these methods polymorphically.
The Codes
The full source codes are listed below. There are 2 files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.turreta.kotlin.extension class Automobile { var make: String? = null var year: Int? = null fun startEngine() { println("Behavior to start the engine") } fun pressBreak() { println("Behavior to stop") } fun pressAccelerator() { println("Behavior to accelerate") } } |
[wp_ad_camp_4]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.turreta.kotlin.extension fun Automobile.startHazardBlinkers() { println("Car ${this.make} [${this.year}] - Hazard blinkers are now on!") println("*** Pressing emergency break no!") this.pressBreak() } fun Automobile.stoptHazardBlinkers() { println("Car ${this.make} [${this.year}] - Hazard blinkers are now off!") } fun main(args: Array<String>) { val toyota: Automobile = Automobile() toyota.make = "Toyota Vios" toyota.year = 2016 toyota.startEngine() toyota.pressAccelerator() toyota.pressBreak() toyota.startHazardBlinkers() toyota.stoptHazardBlinkers() } |
References
[wp_ad_camp_5]