Kotlin functions support default and named arguments, expressions as bodies, and lambdas as values. Higher-order functions take or return functions (map, your own retry). Trailing lambdas sit outside the parentheses. inline with reified recovers generic types that the JVM would erase.
This Kotlin cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.
Quick reference
- Default:
fun greet(name: String = "there"). - Named:
greet(name = "Humza")at the call site. - Single expr:
fun square(n: Int) = n * n. - Lambda:
val add = { a: Int, b: Int -> a + b }anditfor one parameter. - Trailing:
list.filter { it.active }— last lambda outside(). - Receiver:
buildString { append("x") }is a lambda with receiver (this). - inline: Inlines the lambda to avoid allocation and allow
reified/ non-local return. - infix:
infix fun String.should(other: String)for DSLs — easy to overuse.
Copy-paste examples
Higher-order retry and a trailing lambda
Keep public APIs named. Use inline only when you need reified types or you have measured a hot path.
inline fun <T> retry(times: Int, block: () -> T): T {
var last: Throwable? = null
repeat(times) {
try {
return block()
} catch (t: Throwable) {
last = t
}
}
throw last ?: IllegalStateException("retry failed")
}
fun titles(rows: List<String>): List<String> =
rows.filter { it.isNotBlank() }.map { it.trim() }Common mistakes
- Mutating a default
listOf()-style default argument — uselist: List<T> = emptyList()and do not mutate the default. - Making every helper
inlineand exploding bytecode. - Nested lambdas with multiple
itbindings you cannot read later. - Java-style overloads of 8 methods instead of one defaulted function.
FAQ
- What is BLOGPH0 ? The implicit name of a single lambda parameter. Name it (
item ->) when nested or unclear.
- Unit vs void? Kotlin functions return
Unitby default. You can omit the type. Javavoidmaps toUnit.
- When do I use BLOGPH0 ? When an inline function must pass a lambda to another non-inline API. The compiler will tell you.
Related Kotlin cheat sheets
Build with this stack
When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds mobile app development and also sells ready-made source code. Request a quote and tell us the stack.
Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.