# scope function

Published articles for scope function.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Scopes and Scope Functions

DevFeed: [Scopes and Scope Functions](<https://devfeed.tech/articles/scopes-and-scope-functions-25063.md>)

Original publisher: [Read original article](<https://typealias.com/start/kotlin-scopes-and-scope-functions/>)

Author: author@typealias.com (Dave Leeds)

Published: 2022-08-22T00:00:00Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Library](<https://devfeed.tech/topics/library.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [also](<https://devfeed.tech/tags/also.md>), [apply](<https://devfeed.tech/tags/apply.md>), [classes](<https://devfeed.tech/tags/classes.md>), [code](<https://devfeed.tech/tags/code.md>), [introduction](<https://devfeed.tech/tags/introduction.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [learn](<https://devfeed.tech/tags/learn.md>), [learn-to-program](<https://devfeed.tech/tags/learn-to-program.md>), [let](<https://devfeed.tech/tags/let.md>), [library](<https://devfeed.tech/tags/library.md>), [programming](<https://devfeed.tech/tags/programming.md>), [run](<https://devfeed.tech/tags/run.md>), [scope](<https://devfeed.tech/tags/scope.md>), [scope-function](<https://devfeed.tech/tags/scope-function.md>), [scope-functions](<https://devfeed.tech/tags/scope-functions.md>), [shadowing](<https://devfeed.tech/tags/shadowing.md>), [standard](<https://devfeed.tech/tags/standard.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>), [variable](<https://devfeed.tech/tags/variable.md>), [visibility](<https://devfeed.tech/tags/visibility.md>), [with](<https://devfeed.tech/tags/with.md>)

### AI overview

A Kotlin tutorial chapter introduces scopes as regions of code where variables, functions, and classes can be declared, then begins explaining Kotlin's five scope functions from the standard library.

### Source excerpt

In the last chapter, we learned how to create extension functions, which can be called using dot notation. In this chapter, we'll learn about scope functions, which are five particular functions that Kotlin gives us in its standard library. Before we can understand scope functions, though, it helps to first understand scopes. Introduction to Scopes In Kotlin, a scope is a section of code where we can declare new variables, functions, classes, and more.

## Using Kotlin's with Scope Function and Receiver Lambdas

DevFeed: [Using Kotlin's with Scope Function and Receiver Lambdas](<https://devfeed.tech/articles/with-the-receiver-in-scope-26030.md>)

Original publisher: [Read original article](<https://elizarov.medium.com/with-the-receiver-in-scope-7b52bdcca6e9?source=rss-4762e889f8fc------2>)

Author: Roman Elizarov

Published: 2020-07-01T14:33:13Z

Content type: tutorial

Language: en

Sources: [Stories by Roman Elizarov on Medium](<https://devfeed.tech/sources/stories-by-roman-elizarov-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Programming language](<https://devfeed.tech/topics/programming-language.md>), [Library](<https://devfeed.tech/topics/library.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [context](<https://devfeed.tech/tags/context.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [lambda](<https://devfeed.tech/tags/lambda.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-language](<https://devfeed.tech/tags/programming-language.md>), [receiver](<https://devfeed.tech/tags/receiver.md>), [scope-function](<https://devfeed.tech/tags/scope-function.md>), [scopes](<https://devfeed.tech/tags/scopes.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>)

### AI overview

The article explains how Kotlin's with scope function reduces repetitive object references and groups related initialization in a distinct block. It also introduces extension functions and lambdas with a receiver as ways to access object members without repeating the object's name.

### Source excerpt

Photo by Ben Wicks on UnsplashRepetitio est mater studiorum (Latin, repetition is the mother of all learning). Repetition is great for study, but a bane of software development. Repetitive code is boring and error-prone. Let's look at a hypothetical example inspired by imperative UI frameworks that are still being used a lot nowadays, even though their heyday is past. We might find ourselves having to write code like this: applicationWindow.title = "Just an example" applicationWindow.position = FramePosition.AUTO applicationWindow.content = createContent() applicationWindow.show() Referencing applicationWindow object over and over again is very explicit but it is not pretty. We can make this code better using with scope function from the Kotlin standard library: with(applicationWindow) { // this: ApplicationWindow title = "Just an example" position = FramePosition.AUTO content = createContent() show() } This code now has more lines, but the number of lines is not the key metric you should be looking at when judging the code. The code looks cleaner. It groups all the initialization of the applicationWindow object into a separate, syntactically distinct block. It directly represents the developer's intent to perform all of these actions on applicationWindow together. The block of code inside of with(x) { ... } is easily recognizable by any developer trained in a mainstream object-oriented language -- it is similar to the code you encounter inside a method of the corresponding class where this refers to the method's receiver object and all unqualified references like title refer to this object's properties and methods. In the Kotlin programming language writing a method is not the only way to get access to object members without having to repeat the object's name. Kotlin has support for extension functions that allow us to write a method-like looking code outside of the class body: fun ApplicationWindow.configure() { // this: ApplicationWindow title = ... // no need to

## with() (scope function)

DevFeed: [with() (scope function)](<https://devfeed.tech/articles/with-scope-function-25030.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/with/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-09-14T01:00:44Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [guide](<https://devfeed.tech/tags/guide.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-standard-library](<https://devfeed.tech/tags/kotlin-standard-library.md>), [programming](<https://devfeed.tech/tags/programming.md>), [receiver](<https://devfeed.tech/tags/receiver.md>), [scope-function](<https://devfeed.tech/tags/scope-function.md>), [scope-functions](<https://devfeed.tech/tags/scope-functions.md>), [with](<https://devfeed.tech/tags/with.md>)

### AI overview

An overview of Kotlin's with() scope function, including how it accepts a receiver as an argument, makes that receiver available inside a code block, and returns the block's result. It also covers an initialization-and-execution use case and compares with() with related scope functions.

### Source excerpt

Overview The with() function is a scope function found in the Kotlin standard library. Unlike let(), also(), run() with receiver, and apply(), with() is not an extension function, but instead accepts the receiver as an argument. Example val size = with("Hello") { println(this) length } In this example, the string "Hello" is printed, and then its length is assigned to the size variable. Characteristics When the with() extension function executes:

## apply() (scope function)

DevFeed: [apply() (scope function)](<https://devfeed.tech/articles/apply-scope-function-25008.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/apply/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-09-04T20:06:46Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Library](<https://devfeed.tech/topics/library.md>), [Code](<https://devfeed.tech/topics/code.md>), [Android](<https://devfeed.tech/topics/android.md>)

Tags: [also](<https://devfeed.tech/tags/also.md>), [apply](<https://devfeed.tech/tags/apply.md>), [code](<https://devfeed.tech/tags/code.md>), [extension-function](<https://devfeed.tech/tags/extension-function.md>), [function](<https://devfeed.tech/tags/function.md>), [guide](<https://devfeed.tech/tags/guide.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-standard-library](<https://devfeed.tech/tags/kotlin-standard-library.md>), [let](<https://devfeed.tech/tags/let.md>), [library](<https://devfeed.tech/tags/library.md>), [object](<https://devfeed.tech/tags/object.md>), [overview](<https://devfeed.tech/tags/overview.md>), [programming](<https://devfeed.tech/tags/programming.md>), [receiver](<https://devfeed.tech/tags/receiver.md>), [scope-function](<https://devfeed.tech/tags/scope-function.md>), [scope-functions](<https://devfeed.tech/tags/scope-functions.md>), [standard](<https://devfeed.tech/tags/standard.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>), [val](<https://devfeed.tech/tags/val.md>), [variable](<https://devfeed.tech/tags/variable.md>)

### AI overview

An overview of Kotlin's apply() scope function, including its receiver and return value, with examples of using it to initialize objects and configure Android code.

### Source excerpt

Overview The apply() function is a commonly-used scope function defined in the Kotlin standard library. It's frequently used for initializing an object after instantiation. Example val size = "Hello".apply { println(this) }.length In this example, the string "Hello" is printed, and then its length is assigned to the size variable. Characteristics When the apply() extension function executes: the receiver object that .apply() is called upon is available as this the result will be the receiver object Initializing an Object It's common to use apply() to initialize an object after it's been created.

## let() (scope function)

DevFeed: [let() (scope function)](<https://devfeed.tech/articles/let-scope-function-25019.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/let/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-09-04T19:54:18Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Library](<https://devfeed.tech/topics/library.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [extension-function](<https://devfeed.tech/tags/extension-function.md>), [function-reference](<https://devfeed.tech/tags/function-reference.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-standard-library](<https://devfeed.tech/tags/kotlin-standard-library.md>), [lambda](<https://devfeed.tech/tags/lambda.md>), [let](<https://devfeed.tech/tags/let.md>), [programming](<https://devfeed.tech/tags/programming.md>), [receiver](<https://devfeed.tech/tags/receiver.md>), [scope](<https://devfeed.tech/tags/scope.md>), [scope-function](<https://devfeed.tech/tags/scope-function.md>), [transformation](<https://devfeed.tech/tags/transformation.md>)

### AI overview

A reference guide to Kotlin's let() scope function. It explains how the receiver object is passed as the lambda argument, how let() returns the code block's result, and how it can transform objects. It also compares let() with direct property or function access, extension functions, and map().

### Source excerpt

Overview The let() function is one of a handful of scope functions defined in the Kotlin standard library. Example val size = "Hello".let { println(it) it.length } In this example, the string "Hello" is printed, and then its length is assigned to the size variable. Characteristics When the let() extension function executes: the receiver object that .let() is called upon is available as the lambda argument - by default, named it the result of let() will be the result of the code block passed to it Transformation The let() method is frequently used to transform an object.

## also() (scope function)

DevFeed: [also() (scope function)](<https://devfeed.tech/articles/also-scope-function-25007.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/also/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-09-04T19:41:30Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>)

Tags: [also](<https://devfeed.tech/tags/also.md>), [extension-function](<https://devfeed.tech/tags/extension-function.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [scope-function](<https://devfeed.tech/tags/scope-function.md>), [scope-functions](<https://devfeed.tech/tags/scope-functions.md>)

### AI overview

A Kotlin tutorial explaining the also() scope function. It shows that also() exposes the receiver as the lambda argument, conventionally named it, and returns the receiver object.

### Source excerpt

Overview The also() function is a scope function that was introduced in Kotlin 1.1. Example val size = "Hello".also { println(it) }.length In this example, the string "Hello" is printed, and then its length is assigned to the size variable. Characteristics When the also() extension function executes: the receiver object that .also() is called upon is available as the lambda argument - by default, named it the result will be the receiver object Create, Pass, and Evaluate A prime use case for also() is the pattern where you need to pass an empty object to another function for initialization.

## run() with receiver (scope function)

DevFeed: [run() with receiver (scope function)](<https://devfeed.tech/articles/run-with-receiver-scope-function-25022.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/run-with-receiver/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-09-03T20:04:25Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [extension-function](<https://devfeed.tech/tags/extension-function.md>), [function](<https://devfeed.tech/tags/function.md>), [http](<https://devfeed.tech/tags/http.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [library](<https://devfeed.tech/tags/library.md>), [programming](<https://devfeed.tech/tags/programming.md>), [receiver](<https://devfeed.tech/tags/receiver.md>), [scope-function](<https://devfeed.tech/tags/scope-function.md>), [scope-functions](<https://devfeed.tech/tags/scope-functions.md>)

### AI overview

This tutorial explains Kotlin's run() extension function, one of the scope functions in the Kotlin standard library. It covers the receiver object, the function's result, and using run() to initialize and execute an object, including an HTTP client example.

### Source excerpt

Overview There are two different run() functions defined in Standard.kt in the Kotlin standard library, among a handful of scope functions that are frequently used in Kotlin programming. This article describes the one that's an extension function. Example val size = "Hello".run { println(this) this.length } In this example, the string "Hello" is printed, and then its length is assigned to the size variable. Characteristics When the run() extension function executes: