August 2017
Intermediate to advanced
440 pages
10h
English
Infix functions were introduced in Chapter 4, Classes and Objects, but they can be defined not only as member classes, but also as extension functions. This makes it possible to create an infix extension function to any object. One of this kinds of extension functions is to, which was briefly described in Chapter 2, Laying a Foundation. Now we have the knowledge needed to understand its implementation. This is how to is defined:
infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)
This makes it possible to place to between any two objects and make this way Pair with them:
println( 1 to 2 == Pair(1, 2) ) // Prints: true
Note that the fact that we can make infix extension functions allows us to define infix functions ...
Read now
Unlock full access