Infix calls for named methods
Infix calls are features of Kotlin that allow us to create more fluid and readable code. They allow us to write code that is closer to natural human language. We have already seen the infix methods in Chapter 2, Laying a Foundation, which allowed us to easily create an instance of a Pair
class. Here is a quick reminder:
var pair = "Everest" to 8848
The Pair
class represents a generic pair of two values. There is no meaning attached to values in this class, so it can be used for any purpose. Pair
is a data class, so it contains all data class methods (equals
, hashCode
, component1
, and so on). Here is a definition of the Pair
class from the Kotlin standard library:
public data class Pair<out A, out B>( // 1 public val first: A, public val second: B ) : Serializable { public override fun toString(): String = "($first, $second)" // 2 }
- The meaning of this
out
modifier when used behind a generic type will be...