Skip to content
tezvyn:

Explain Kotlin's scope functions: let, run, with, apply, also

Source: kotlinlang.orgEasyHow cards are made

Explain Kotlin's scope functions: let, run, with, apply, also

This tests your grasp of idiomatic Kotlin and the two key differentiators: context object (this/it) and return value (object/lambda result). A great answer defines these axes, categorizes each function, and provides a clear example.

What's really being asked

This question isn't about rote memorization. It tests your understanding of idiomatic Kotlin and code readability. The interviewer is checking if you can articulate the decision-making framework for choosing a scope function, not just list their definitions. For a senior role, this signals your ability to write clean, maintainable code and guide team conventions. It separates candidates who use features because they can from those who use them because they should.

The full answer

First, establish the two key axes of differentiation: the context object reference and the return value. Second, explain the context object: run, with, and apply reference the context as a lambda receiver (this), making them good for calling functions on the object itself. let and also reference it as a lambda argument (it), which is useful to avoid ambiguity or when passing it to another function. Third, explain the return value: apply and also return the context object itself, making them perfect for chaining or for object configuration. let, run, and with return the result of the lambda expression. Finally, provide the classic use case for apply: object configuration, especially during initialization.

The mistakes people make

One major red flag is simply listing the functions without the this/it and return value framework. Another is confusing the return values, for example, trying to assign the result of a calculation inside an apply block to a variable. Senior candidates should also know when not to use a scope function; using one where a simple variable assignment would be clearer is a sign of inexperience. Finally, advocating for nesting scope functions is a significant anti-pattern, as it dramatically hurts readability.

What usually comes next

Expect follow-ups that probe practical application. For example: "When would you choose let over a simple if (x != null) check?" (Answer: When you need to chain multiple operations on the non-null object). Or, "Give an example where also is better than apply." (Answer: For side-effects that shouldn't alter the chain, like logging the object's state mid-operation). A question about code review, like "When have you seen scope functions abused?", tests your real-world experience.

A concrete example

The canonical use case for apply is configuring an object after creation. A perfect example is setting arguments on an Android Fragment: val fragment = MyFragment().apply { arguments = Bundle().apply { putString("USER_ID_KEY", userId) } }. This is more concise and readable than creating a separate val bundle and then assigning it.

Interview question

You need to create an object and immediately set several of its properties in a single, concise block. Which scope function is most idiomatic for this configuration task?

  • a.also
  • b.run
  • c.let
  • d.applyCorrect
Why?

The `apply` function is designed for object configuration. It returns the context object itself, making it perfect for initialization, while `let` and `run` return the lambda result.

Just read this? Test yourself on what you have been reading.

Read the original → kotlinlang.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles