# 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