# Scope functions

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

<figure><img src="/files/IljFo8FsMbILQd0gq1Fa" 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="/files/PlIIQQH5VMX6RsICHBz9" 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://amartyushov.gitbook.io/tech/programming-languages/kotlin/scope-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
