> For the complete documentation index, see [llms.txt](https://amartyushov.gitbook.io/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://amartyushov.gitbook.io/tech/programming-languages/kotlin/scope-functions.md).

# Scope functions

[Good overview about scopes and scope functions](https://typealias.com/start/kotlin-scopes-and-scope-functions/)

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2Fv0gGvI04FarQL8QZspqX%2FScreenshot%202022-09-06%20at%2009.45.30.png?alt=media&amp;token=46e6f868-e2f9-4e7e-9658-abbe37b70d73" alt=""><figcaption></figcaption></figure>

## Shadowing and Implicit Receivers <a href="#shadowing-and-implicit-receivers" id="shadowing-and-implicit-receivers"></a>

```kotlin
class Person(val name: String) {
    fun sayHello() = println("Hello!")
}

class Dog(val name: String) {
    fun bark() = println("Ruff!")
}

val person = Person("Julia")
val dog = Dog("Sparky")

with(person) {
    with(dog) {
        println(name) // Prints Sparky from the dog object
        bark()        // Calls bark() on the dog object
        sayHello()    // Calls sayHello() on the person object
    }
}
```

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FejBxavNLnt5d8lYHv0Wr%2FScreenshot%202022-09-06%20at%2009.36.18.png?alt=media&amp;token=805c8dd4-4981-4406-8d8b-87f34c3a9247" alt=""><figcaption></figcaption></figure>

```kotlin
with(person) {
    with(dog) {
        println(this.name) // Prints Sparky
        this.bark()        // Calls bark() on the dog object
        this.sayHello()    // Compiler error - Unresolved reference: sayHello
    }
}
```

1. When using `this`, it will ***only*** refer to the exact implicit receiver in that scope.
2. When **omitting** `this`, the effective receiver is a **combination of the implicit receivers**, from the innermost to the outermost scope.
