Inline keyword without higher-order function in Collections.kt

ilya-g in this commit added inline keyword to many functions in Collections.kt.

public inline fun <T> listOf(): List<T> = emptyList()

What is the benefit or purpose of inline keyword with a function which is not a higher-order function? How lack of this keyword would introduce runtime overhead at call-sites?

These functions are annotated with InlineOnly, which tells the compiler that the function should NOT be part of the generated binaries (only their inlined version should be generated). For the snippet you posted, this means that the function listOf does not exist past compile-time.

The obvious benefit to this is that a function that does not exist does not take up any space, this allows the Kotlin team to introduce some nice but (arguably) redundant functions without bloating the size of the Kotlin API.


One thing to note is that InlineOnly is restricted to the standard library so you can’t use it yourself. There are a few hacks to get around that restriction though.

1 Like