> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Delegated Property

A delegated property in Kotlin is a property whose getter and setter logic is abstracted and routed to a separate object, known as the *delegate*. Instead of implementing the backing field and accessors directly within the property declaration, the Kotlin compiler delegates read and write operations to specific operator functions defined on the delegate instance.

## Syntax

The delegation is established using the `by` keyword:

```kotlin theme={"dark"}
val/var <property name>: <Type> by <delegate>
```

This syntax applies to member properties, top-level properties, extension properties, and local delegated properties (variables declared inside functions or blocks).

## Compiler Mechanics

When you declare a delegated property, the Kotlin compiler generates a hidden backing field that holds the instance of the delegate. It then translates all property accesses into method calls on that delegate:

* Reading the property translates to a call to the `getValue()` operator.
* Writing to the property (if it is a `var`) translates to a call to the `setValue()` operator.

## Operator Function Signatures

To act as a delegate, an object must provide specific operator functions. These functions can be implemented directly as member functions or as extension functions.

For a read-only property (`val`), the delegate must provide a `getValue` operator:

```kotlin theme={"dark"}
import kotlin.reflect.KProperty

class StringDelegate {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "Delegated Value"
    }
}
```

For a mutable property (`var`), the delegate must provide both `getValue` and `setValue` operators:

```kotlin theme={"dark"}
import kotlin.reflect.KProperty

class MutableStringDelegate {
    private var backingValue: String = "Initial"

    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return backingValue
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        backingValue = value
    }
}
```

### Parameter Breakdown

* **`thisRef`**: Represents the receiver of the property. Its type must be the same as, or a supertype of, the expected receiver.
  * For a **member property**, it is the object in which the property is declared.
  * For an **extension property** (whether top-level or member), it is the extension receiver.
  * For a **top-level non-extension property** or a **local delegated variable**, it is `Nothing?`.
* **`property`**: Holds metadata about the property being accessed, such as its name. It must be of type `KProperty<*>` or a supertype.
* **`value`** (Setter only): The new value being assigned. Its type must be the exact type of the property or its supertype.

## Standard Library Interfaces

Instead of writing the operator functions from scratch, developers can implement the `ReadOnlyProperty` or `ReadWriteProperty` interfaces provided by the Kotlin standard library. This ensures type safety and correct method signatures at compile time.

```kotlin theme={"dark"}
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class TypedDelegate : ReadWriteProperty<Any?, String> {
    override fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "Value"
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        // Assignment logic
    }
}
```

## Built-in Standard Library Delegates

The Kotlin standard library provides several factory methods and implementations for common delegation mechanics:

### `lazy`

The `lazy()` function returns a `Lazy<T>` instance that serves as a delegate for implementing a read-only property. The first call to `getValue()` executes the lambda passed to `lazy()` and records the result. Subsequent calls return the recorded result without re-executing the lambda.

```kotlin theme={"dark"}
val lazyValue: String by lazy {
    "Computed Value"
}
```

### `Delegates.observable`

Returns a `ReadWriteProperty` that intercepts assignments. The delegate takes an initial value and a callback lambda. The callback is invoked *after* the assignment to the backing field completes, receiving the property metadata, the old value, and the new value.

```kotlin theme={"dark"}
import kotlin.properties.Delegates

var observableValue: String by Delegates.observable("Initial") { property, old, new ->
    // Executes after modification
}
```

### `Delegates.vetoable`

Returns a `ReadWriteProperty` that intercepts assignments *before* they are applied to the backing field. If the callback lambda returns `true`, the assignment proceeds. If it returns `false`, the new value is discarded, and the property retains its old value.

```kotlin theme={"dark"}
import kotlin.properties.Delegates

var vetoableValue: Int by Delegates.vetoable(0) { property, old, new ->
    new >= 0 // Rejects negative assignments
}
```

### Map Delegation

Properties can be delegated directly to a `Map` (for read-only `val` properties) or a `MutableMap` (for mutable `var` properties). The delegate uses the property's name (via the `KProperty` metadata) as the string key to retrieve or update the corresponding value in the map.

```kotlin theme={"dark"}
class User(val map: Map<String, Any?>) {
    val name: String by map
    val age: Int by map
}
```

## The `provideDelegate` Operator

Kotlin supports an optional `provideDelegate` operator. If the object on the right side of the `by` keyword defines this operator, the compiler calls it to create the actual delegate instance during the property's initialization. This allows for validation or custom logic to be executed at the exact moment the property is bound to the delegate, rather than waiting for the first read/write operation.

```kotlin theme={"dark"}
import kotlin.reflect.KProperty

class DelegateProvider {
    operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): StringDelegate {
        return StringDelegate()
    }
}

class Example {
    val myProperty: String by DelegateProvider()
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
