# Choosing between with() and run()

DevFeed: [Choosing between with() and run()](<https://devfeed.tech/articles/choosing-between-with-and-run-25032.md>)

Original publisher: [Read original article](<https://typealias.com/guides/choosing-between-with-and-run/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-09-14T01:23:30Z

Content type: comparison

Language: en

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

Topics: [scope function](<https://devfeed.tech/topics/scope-function.md>)

Tags: [bytecode](<https://devfeed.tech/tags/bytecode.md>), [decompiler](<https://devfeed.tech/tags/decompiler.md>), [extension-function](<https://devfeed.tech/tags/extension-function.md>), [intellij](<https://devfeed.tech/tags/intellij.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [library](<https://devfeed.tech/tags/library.md>), [nulls](<https://devfeed.tech/tags/nulls.md>), [programming](<https://devfeed.tech/tags/programming.md>), [readability](<https://devfeed.tech/tags/readability.md>), [receiver](<https://devfeed.tech/tags/receiver.md>), [run](<https://devfeed.tech/tags/run.md>), [safe-call-operator](<https://devfeed.tech/tags/safe-call-operator.md>), [scope-functions](<https://devfeed.tech/tags/scope-functions.md>), [with](<https://devfeed.tech/tags/with.md>)

## AI overview

Kotlin's `with()` and receiver `run()` both expose the object as `this` inside the block and return the block's result; the excerpt says they compile almost identically. The key practical difference shown is that extension `run()` can be called with a safe-call (`?.`), so a null receiver skips the block and yields null. Passing a nullable value to `with()` still executes its block, so it does not provide the same null interception. The excerpt begins a readability discussion but cuts off before stating its conclusion.

## Source excerpt

In Kotlin's standard library, the Standard.kt file contains a number of scope functions that put a receiver object into a new scope as either this or as an argument (e.g., it). The with() function tends to be a bit of an outlier. The others are all extension functions, so they're invoked on an object. with(), on the other hand, takes its receiver (the object that it's operating upon) as an argument.