August 2017
Intermediate to advanced
440 pages
10h
English
Extension functions are just functions with a receiver as the first parameter, so their calls are resolved at compile time by the type on which the function is invoked. For example, when there are extension functions for both superclass and subclass, then the extension functions that will be chosen during invocation depend on the type of property on which we are operating. Here is an example:
abstract class A
class B: A()
fun A.foo() { println("foo(A)") }
fun B.foo() { println("foo(B)") }
val b = B()
b.foo() // prints: foo(B)
(b as A).foo() // 1, prints: foo(A)
val a: A = b
a.foo() // 1, prints: foo(A)
Read now
Unlock full access