Skip to content
tezvyn:

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

Source: kotlinlang.orgEasyHow cards are made

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

This tests your grasp of idiomatic Kotlin. A great answer categorizes functions by their context object (this/it) and return value (object/lambda result). Provide a clear use case for apply (object configuration).

What's really being asked

This question tests your understanding of idiomatic Kotlin beyond basic syntax. The interviewer is evaluating your ability to write clean, concise, and readable code by choosing the right tool for the job. They want to see if you understand the two primary axes of differentiation: 1) the context object reference (this vs. it) and 2) the return value (the context object itself vs. the result of the lambda). This demonstrates a deeper understanding of expression-oriented programming and code style.

The full answer

A strong answer will first establish the framework for comparison. First, explain the two key differences: context object (this or it) and return value (context object or lambda result). Second, group the functions accordingly: apply and also return the context object, while let, run, and with return the lambda result. Third, differentiate within those groups: apply, run, and with use this as the receiver; also and let use it as an argument. Fourth, provide a canonical use case for each, especially apply for object configuration, as requested.

The mistakes people make

A weak answer is just a list of function definitions without a clear comparative structure. A major red flag is being unable to articulate the this/it and return value differences, as this is the core of the problem. Another is suggesting complex, nested scope function chains, which is an anti-pattern that harms readability. Finally, candidates who can't provide a simple, practical example for apply (like configuring a Paint or Intent object) show a lack of practical application. Don't just say apply is for object configuration; show it.

What usually comes next

"When would you prefer run over let?" (Answer: When you need to call multiple methods on an object and return a custom result, as this makes the code cleaner than repeating it). "When should you AVOID using scope functions?" (Answer: When they make the code harder to read, such as in complex nested chains or when a simple local variable would be clearer). "Refactor this code block to use a scope function."

A concrete example

For apply, the classic example is object initialization where you need to set multiple properties and then return the configured object itself for assignment or chaining. For instance: val newDialog = AlertDialog.Builder(context).apply { setTitle("Confirm"); setMessage("Are you sure?"); setPositiveButton("OK", null) }.create(). This is far more concise than creating the builder variable and then calling setters on it on separate lines before calling create().

Interview question

When configuring an object by setting multiple properties and needing to return the configured object itself, which Kotlin scope function is most appropriate?

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

apply is specifically designed for object configuration, allowing direct access to properties via 'this' and returning the configured object itself. run and let return the lambda's result, while also uses 'it' as an argument, making 'apply' the idiomatic choice for this scenario.

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