January 2018
Intermediate to advanced
434 pages
14h 1m
English
To understand the preceding example, we need to understand the also function in Kotlin. The also function takes the receiver, performs some operation, and returns the receiver. In simple words, it passes an object and returns the same object.
Applying the also function on an object is like saying "do this as well" to that object.
So, we called the also function on b, did an operation (assigning the value of a to b), and then returned the same receiver that we got as an argument:
var a = 1 var b = 2a = b.also { b = a // p println("it=$it : b=$b : a=$a") // prints it=2:b=1:a=1 }println(a) // print 2println(b) // print 1
Read now
Unlock full access