Predict the output
What does this print?
fun main() {
val a = "kotlin".let { it.length }
val b = "kotlin".also { it.length }
val c = StringBuilder().apply { append("hi") }.toString()
println("$a $b $c")
}Hint 1 of 1
Which of these return the receiver?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 6 kotlin hi
Correct. let returns the lambda result, also returns the receiver, apply returns the builder.
Why not the others?
- B 6 6 hi. also returns the original object, not the lambda's value.
- C kotlin kotlin hi. let returns the lambda's result, the length.
- D 6 kotlin StringBuilder. toString() turns the builder into "hi".