Functional Features
Kotlin's functions-as-values layer: lambdas and their function types, method references, higher-order functions, and the inline machinery that keeps them cheap. The performance half of this area is what distinguishes a strong answer.
part ofKotlinoverview, primer and where to startread it →on this pageshowhide
explore
- How Kotlin Does Functions-as-Values5 questions
- Lambdas & Anonymous Functions25 questions
- Lambda Syntax & Implicit it5 questions
- Trailing-Lambda Convention5 questions
- Closures & Captured Variables5 questions
- Anonymous Functions5 questions
- Function Types20 questions
- Plain Function Types (A)->B5 questions
- Function Types with Receiver A.()->B5 questions
- Nullable & suspend Function Types5 questions
- Function-Type Typealiases5 questions
- Callable References15 questions
- Function References ::5 questions
- Bound vs Unbound References5 questions
- Property & Constructor References5 questions
- Inline Machinery30 questions
- inline Functions5 questions
- noinline5 questions
- crossinline5 questions
- Non-Local Returns from Inline Lambdas5 questions
- reified Type Parameters5 questions
- Inline Cost & Codegen Tradeoffs5 questions
- Higher-Order Functions15 questions
- HOF Declaration & Usage5 questions
- Composing Functions5 questions
- Currying & Partial Application Idioms5 questions
questions
110 · 6 sectionsIn Kotlin, what does a function type like (Int) -> String mean, and how can you store a function in a variable?
basics
~20 sA function type describes a function's inputs and output. (Int) -> String means: takes an Int, returns a String. You can put such a function in a variable and call it later like a normal function.
What is a higher-order function in Kotlin? Show how to write one that takes a function as a parameter and one that returns a function.
basics
~10 sA higher-order function takes a function as an argument, returns a function, or both. For example, map takes a transform function. You declare the parameter with a function type and call it inside.
What is the difference between a lambda, an anonymous function, and a callable (function/property) reference in Kotlin, and when would you use each?
basics
~20 sAll three produce a function value. A lambda is the short { } form. An anonymous function uses fun(...) {...} with an explicit return type. A reference like ::foo points at existing code instead of rewriting it.
What object does a Kotlin lambda actually become at runtime, and what are the performance implications of passing functions as values?
basics
~10 sA lambda becomes an object implementing a FunctionN interface (like Function1) with an invoke method. Creating these objects and capturing variables can add allocations, which inline functions can remove.
How do Kotlin function types relate to Java functional (SAM) interfaces, and when does SAM conversion apply?
basics
~10 sJava interfaces with one method (like Runnable) can accept a lambda through SAM conversion. Kotlin's own function types are different objects, but you can mark a Kotlin interface 'fun interface' to allow lambdas too.
What does it mean that a Kotlin lambda 'closes over' a variable, and what can a lambda do with a captured local variable that a Java lambda cannot?
basics
~10 sA Kotlin lambda can read and remember variables from the function around it. Unlike Java, it can also change a captured var, not just read it.
What is the basic brace syntax of a Kotlin lambda, and what does the implicit `it` refer to?
basics
~20 sA lambda is code in curly braces: { x -> body }. When it takes exactly one parameter you can skip naming it and use the built-in name it instead, like list.map { it * 2 }.
In a multi-line Kotlin lambda, how is the value the lambda produces determined? Show how this works with a map call.
basics
~10 sThe last line in the lambda's body is its result automatically. You don't write a return keyword. Whatever that final line evaluates to is what the lambda hands back.
What is the trailing-lambda convention in Kotlin, and how do you write `list.filter { it > 0 }` using it?
basics
~20 sIf the last argument to a function is a lambda, you can write it outside the parentheses. So instead of filter({ it > 0 }) you write filter { it > 0 }, which reads more cleanly.
How does a bare 'return' behave inside an anonymous function versus inside a lambda passed to forEach? Explain local vs non-local return.
basics
~20 sIn an anonymous function, 'return' exits just that function. In a lambda, a bare 'return' exits the whole enclosing function (a non-local return). That different behavior is the main reason to pick an anonymous function.
You have a property `val onClick: (() -> Unit)?`. How do you call it safely when it might be null, and what is the difference between `onClick?.invoke()` and `onClick()`?
basics
~10 sUse onClick?.invoke(). It runs the function only if it is not null, otherwise it does nothing. Calling onClick() directly would not compile because the type may be null.
How do you read and write a plain Kotlin function type such as (Int, Int) -> String, and what does () -> Unit mean?
basics
~20 sIt describes a value that is itself a function. The part in parentheses lists the input types, the arrow points to the result type. () -> Unit means: takes no input and returns nothing useful.
What is a receiver function type like String.() -> Unit in Kotlin, and how does it differ from a plain function type (String) -> Unit?
basics
~20 sA receiver function type is a lambda that runs 'inside' an object, so you can use this and call its members directly without naming it. A plain function type just takes the object as a normal parameter you must name.
What is a function-type typealias in Kotlin, and why would you declare something like `typealias Handler = (Event) -> Unit`?
basics
~10 sIt gives a short, meaningful name to a function type. Instead of writing (Event) -> Unit everywhere, you write Handler. It is just an alias, so the two names mean exactly the same type.
What is a suspend function type such as `suspend () -> T`, and where can a value of that type be invoked?
basics
~20 sIt is the type of a function that can suspend, like a lambda you can pause and resume. You can only call it from inside a coroutine or another suspend function, not from ordinary code.
What is the difference between a bound reference like `instance::method` and an unbound reference like `Type::method` in Kotlin?
basics
~10 sA bound reference already remembers which object to call the method on, so you just pass arguments. An unbound reference does not; you must pass the object itself as the first argument.
What does the `::` operator do when written as `list.map(::println)`, and how is it different from writing `list.map { println(it) }`?
basics
~20 s::println turns an existing function into a value you can pass around. It does the same job as the lambda { println(it) }, just shorter, because you reuse a function that already exists instead of writing a new one.
What is a property reference like ::name or Person::name in Kotlin, and what can you do with the value you get?
basics
~10 sIt is a value that points to a property instead of reading it. You can pass it to functions like map to read that property from many objects, for example people.map(Person::name).
Given `class Box(val n: Int) { fun scaled(f: Int) = n * f }`, what are the function types of `Box::scaled` and `box::scaled`, and how do you invoke each?
basics
~10 sBox::scaled needs both the box and the factor, so its type is (Box, Int) -> Int. box::scaled already has the box, so it only needs the factor: (Int) -> Int.
How does a constructor reference (::ClassName) work, and how would you use it as a factory passed to map?
basics
~20 s::ClassName is a value that creates new objects of that class. Its parameters match the constructor, so you can pass it where a function is expected, like ids.map(::User) to turn each id into a User.
What does the crossinline modifier do on a lambda parameter of an inline function?
basics
~20 scrossinline marks a lambda of an inline function so it cannot use a non-local return. It is still inlined, but you are not allowed to write 'return' to exit the calling function from inside it.
What does the `inline` modifier do to a Kotlin function, and why is it most useful on functions that take lambda parameters?
basics
~20 sinline tells the compiler to copy the function's body straight into each place it is called, instead of making a real function call. This avoids creating an object for the lambda you pass in, so it runs faster.
Why does Kotlin offer the `inline` keyword for higher-order functions, and what runtime cost does inlining remove?
basics
~20 sNormally passing a lambda creates an extra object and an extra method call. Marking the function inline copies the lambda's code straight into the caller, so no object is created and no extra call happens.
What does the `noinline` modifier do when applied to a lambda parameter of an `inline` function?
basics
~20 sIt tells Kotlin NOT to inline that one lambda. Instead the lambda stays a normal object you can store in a variable or pass to another function, while the other lambdas in the inline function are still inlined.
What is a non-local return in Kotlin, and why does `return` inside `forEach { }` exit the enclosing function?
basics
~20 sA plain return inside a lambda can jump out of the whole surrounding function, not just the lambda. This works in forEach because forEach is an inline function, so the lambda's body is copied directly into your code.
What does it mean to compose two functions in Kotlin, and how would you write an `andThen` helper that runs one function and feeds its result into another?
basics
~10 sComposing means chaining functions so one's output becomes the next's input. An andThen helper takes function f, then g, and returns a new function that does g(f(x)) for any input x.
What is a higher-order function in Kotlin, and how do you declare a function that takes another function as a parameter?
basics
~10 sA higher-order function takes a function as a parameter or returns one. You declare the parameter with a function type like (Int) -> Int, then call it inside the body.
Implement both `compose` and `andThen` as infix extensions on function types and explain why the generic signatures differ in argument order.
basics
~10 sandThen runs the receiver first; compose runs the argument first. Their type parameters differ because the receiver and argument swap which function produces the intermediate value the other consumes.
Why does idiomatic Kotlin usually prefer default and named arguments over currying or hand-rolled partial application? Give a concrete comparison.
basics
~20 sKotlin lets you give parameters default values and pass arguments by name, so you can omit or fix arguments at the call site without building chains of lambdas. It is clearer and avoids extra objects.
What is currying in Kotlin, and how do you encode a two-argument function like add(a, b) as a curried function using returned lambdas?
basics
~20 sCurrying turns a function that takes many arguments into a chain of functions that each take one argument and return the next function. You do it in Kotlin by returning a lambda from a lambda.