tezvyn:

Compare Kotlin apply and let scope functions

Curated by the Tezvyn teamSource: kotlinlang.orgadvanced
Compare Kotlin apply and let scope functions
WHAT IT TESTS

Your grasp of Kotlin scope mechanics.

ANSWER OUTLINE

Apply uses this and returns receiver for configuration; let uses it and returns lambda result for null-safe transforms.

RED FLAG

Calling them interchangeable or ignoring return values.

WHAT THIS TESTS: This question probes whether you understand the mechanical differences between Kotlin scope functions, specifically how the context object is exposed inside the lambda and what the overall expression evaluates to. A senior candidate should demonstrate that apply and let are not interchangeable syntactic sugar but distinct tools chosen based on whether you are configuring an object or transforming a value, and whether the caller needs the original object or the lambda result back.

A GOOD ANSWER COVERS: Four things in order. First, the receiver reference: apply treats the object as a lambda receiver so you access members via this, which you can usually omit; let treats the object as a lambda argument so you access it via the implicit it parameter. Second, the return value: apply returns the context object itself, while let returns whatever the last expression in the lambda evaluates to. Third, canonical use cases: use apply for object configuration or builder-style setup because you want to mutate properties and then return the configured instance; use let for null-safe transformations or to introduce a scoped variable in a long chain because you want to operate on a non-nullable copy and pass the result forward. Fourth, a concise code snippet for each that makes the distinction obvious without needing to run it.

COMMON WRONG ANSWERS: Several patterns raise red flags. One is claiming the difference is only stylistic and that you can always swap them. Another is forgetting that apply returns the receiver, which causes bugs when you chain a transformation and accidentally discard the computed value. A third is using let for heavy receiver-member mutation, which forces repetitive it. prefixes and hurts readability compared to direct member access. A fourth is nesting scope functions without clarifying which this or it belongs to which level, creating confusion for reviewers.

LIKELY FOLLOW-UPS: Interviewers often push deeper by asking when to prefer run over let, or how also differs from apply. They may ask you to refactor a deeply nested apply-inside-let block into something readable, or to explain why with exists as a non-extension alternative. Another common extension is asking about performance: scope functions are inlined so there is zero runtime overhead, and not knowing that can signal shallow understanding. They might also ask you to compare also versus apply since both return the context object but expose it differently.

ONE CONCRETE EXAMPLE: For apply, consider val person = Person("Alice").apply { age = 30; city = "NYC" } where the block configures fields on this and the expression returns the Person instance. For let, consider val length = name?.let { it.trim().uppercase().length } where the block receives the non-null String as it and returns the Int length; if name is null the whole expression evaluates to null. These two snippets show configuration versus transformation and object return versus computed return.

Source: kotlinlang.org

Read the original → kotlinlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Compare Kotlin apply and let scope functions · Tezvyn