Sitemap

Member-only story

Kotlin Scoped Functions Explained Simply: A Complete Guide for Developers

8 min readJul 12, 2025

--

Scope functions like let, run, apply, also, and with are commonly used in Kotlin, but many developers struggle to understand when to use each one. They may look similar but have different behaviors, and using the wrong one can make code harder to read and maintain. This confusion often leads to disagreements in teams and hesitation among newer developers. However, when used properly, scope functions can make your Kotlin code more expressive, safe, and readable. This guide explains the purpose of each function, when to use them, and provides practical examples to help you use them with confidence.

Press enter or click to view image in full size

What Are Scope Functions?

Scope functions are higher-order functions that execute a block of code within the context of an object. Kotlin provides five scope functions: let, run, apply, also, and with.

Each scope function differs in two key ways:

Context object reference How you refer to the object inside the block (this or it)

Return value What the function returns (the object itself or the result of the block)

Understanding these differences is the key to mastering scope functions.

1. The let Function: Transform and Execute Safely

--

--

Ramadan Sayed
Ramadan Sayed

Written by Ramadan Sayed

Software engineer, 9 years in. I write about the parts of software nobody documents — and whatever else I can't stop thinking about.