Curious about how inline functions work. The docs ...
# announcements
a
Curious about how inline functions work. The docs say "The inline modifier affects both the function itself and the lambdas passed to it: all of those will be inlined into the call site." But suppose I have this:
Copy code
val square: (Int) -> Int = { it * it }
val transforms = listOf(square, { it }, { it * 2 }, { 1 })
inline fun printNumbers(transform: (Int) -> Int) {
    for (i in 1..10) println(transform(i))
}
printNumbers(transforms.getRandomElement())
I'd be frightened if the compiler could figure out how to inline the transform in this case. So I guess what I wonder is when does inlining actually take place? Should I read
inline
as "inline if possible"? Or "inline if the function argument is a lambda literal"? Or "inline if the transform argument is effectively final"? The following would inline to
println(i*i)
, right? But what if I made
square
a
var
?
Copy code
printNumbers(square)