Discover how a small change in syntax can make your Kotlin code feel like a breeze to read and write!
Why Trailing lambda convention in Kotlin? - Purpose & Use Cases
Imagine writing a function call in Kotlin that takes a block of code as a parameter. Without the trailing lambda convention, you have to put the lambda inside the parentheses, making the call look cluttered and harder to read.
This manual way makes your code look crowded and less clear, especially when the lambda is long. It's easy to lose track of where the function call ends and the lambda begins, causing confusion and mistakes.
The trailing lambda convention lets you move the lambda expression outside the parentheses if it's the last argument. This makes your code cleaner, easier to read, and more natural to write, just like telling a story step-by-step.
list.filter({ it > 5 })list.filter { it > 5 }This convention enables writing Kotlin code that feels smooth and expressive, improving readability and reducing visual clutter.
When you use Kotlin's standard library functions like filter or map, trailing lambdas make your code look neat and easy to understand, especially in complex data processing.
Manual lambda syntax can make code look cluttered.
Trailing lambda moves the lambda outside parentheses for clarity.
It makes Kotlin code more readable and expressive.
Practice
trailing lambda convention in Kotlin?Solution
Step 1: Understand the trailing lambda convention and compare options
The trailing lambda convention lets you write a lambda expression outside the parentheses if it is the last argument of a function. Only "To allow placing the lambda expression outside the parentheses when it is the last argument" correctly describes this behavior. Other options contradict the convention.Final Answer:
To allow placing the lambda expression outside the parentheses when it is the last argument -> Option AQuick Check:
Trailing lambda = lambda outside parentheses [OK]
- Thinking lambdas must always be inside parentheses
- Confusing trailing lambda with lambda variable declaration
- Believing trailing lambda disallows lambdas as arguments
Solution
Step 1: Identify correct trailing lambda syntax and check other options
Trailing lambda means the lambda is placed outside the parentheses.list.filter { it > 0 }shows this correctly.list.filter({ it > 0 })uses lambda inside parentheses (valid but not trailing).list.filter(it > 0)and D are syntax errors.Final Answer:
list.filter { it > 0 } -> Option AQuick Check:
Trailing lambda = lambda outside parentheses [OK]
- Putting lambda inside parentheses when trailing lambda is expected
- Omitting parentheses incorrectly
- Writing lambda without braces
fun greet(action: () -> String) = println(action())
greet { "Hello, Kotlin!" }Solution
Step 1: Understand the function call and evaluate the output
The functiongreettakes a lambda returning a String and prints its result. The call uses trailing lambda syntax. The lambda returns "Hello, Kotlin!" which is printed byprintln.Final Answer:
Hello, Kotlin! -> Option DQuick Check:
Trailing lambda call prints lambda result [OK]
- Expecting syntax error due to trailing lambda
- Confusing return value with Unit
- Missing that println prints the lambda result
fun calculate(x: Int, operation: (Int) -> Int): Int {
return operation(x)
}
val result = calculate(5) operation { it * 2 }Solution
Step 1: Analyze the function call syntax and correct trailing lambda usage
The function callcalculate(5) operation { it * 2 }is invalid because the trailing lambda must be placed immediately after the parentheses without extra tokens. The correct call iscalculate(5) { it * 2 }. The wordoperationshould not appear between parentheses and lambda.Final Answer:
Trailing lambda must be outside parentheses but parentheses are missing -> Option CQuick Check:
Trailing lambda goes right after parentheses [OK]
- Inserting parameter name between parentheses and lambda
- Forgetting to remove parentheses when using trailing lambda
- Confusing function parameter names with call syntax
fun processNumbers(numbers: List<Int>, filter: (Int) -> Boolean, action: (Int) -> Unit) {
for (number in numbers) {
if (filter(number)) action(number)
}
}Which is the correct way to call
processNumbers using trailing lambda convention for both lambdas?Solution
Step 1: Understand trailing lambda rules for multiple lambdas and apply to function call
Kotlin allows only the last lambda argument to be passed outside parentheses as a trailing lambda. Sinceactionis the last lambda, it can be trailing. The first lambdafiltermust be inside parentheses.processNumbers(listOf(1,2,3), { it > 1 }) { println(it) }correctly placesfilterinside parentheses andactionoutside.Final Answer:
processNumbers(listOf(1,2,3), { it > 1 }) { println(it) } -> Option BQuick Check:
Only last lambda can be trailing [OK]
- Trying to put multiple lambdas outside parentheses
- Swapping lambda order incorrectly
- Putting non-lambda arguments outside parentheses
Ready to prove it?
Pass the test and this pattern moves unknown
