Kotlin Inline Functions


Example

Functions can be declared inline using the inline prefix, and in this case they act like macros in C - rather than being called, they are replaced by the function's body code at compile time. This can lead to performance benefits in some circumstances, mainly where lambdas are used as function parameters.

inline fun sayMyName(name: String) = "Your name is $name" 

One difference from C macros is that inline functions can't access the scope from which they're called:

inline fun sayMyName() = "Your name is $name"

fun main() {
    val name = "Foo"
    sayMyName() # => Unresolved reference: name
}