Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
fun main() {
val a = mutableListOf(1).apply { add(2) }
val b = mutableListOf(1).let { it.add(2) }
println(a)
println(b)
}[1, 2] true
- Expecting
[1, 2] [1, 2]: thinkingletreturns the list.letreturns the lambda's last value, andaddreturnstrue. - Expecting
truefora: thinkingapplyreturns what its lambda computes.applyalways returns the object itself, here the list[1, 2].
- What would
bhold withalsoinstead oflet? - Why does
addon a list returntrue?