Inline & Value Classes in Kotlin
From the Kotlin cheat sheet · Advanced Features · verified Jul 2026
Inline & Value Classes
Performance optimizations
kotlin
// Inline functions
inline fun measureTimeMillis(block: () -> Unit): Long {
val start = System.currentTimeMillis()
block()
return System.currentTimeMillis() - start
}
// No function call overhead
val time = measureTimeMillis {
// Code is inlined here
Thread.sleep(100)
}
// Inline classes (value classes)
@JvmInline
value class UserId(val id: Long) {
init {
require(id > 0) { "Id must be positive" }
}
val displayId: String
get() = "USER_$id"
}
// No boxing overhead at runtime
fun getUser(userId: UserId) {
// userId is passed as primitive long
}
// Crossinline and noinline
inline fun inlineFunc(
crossinline body: () -> Unit, // Can't use return
noinline callback: () -> Unit // Not inlined
) {
val runnable = object : Runnable {
override fun run() {
body() // OK with crossinline
}
}
callback() // Regular function call
}💡 inline eliminates function call overhead
⚡ Value classes wrap primitives without runtime overhead
📌 Contracts tell compiler about function behavior
🟢 Type aliases improve code readability