> ## 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 Observable Delegation

Kotlin observable delegation is a property delegation mechanism provided by the standard library (`kotlin.properties.Delegates`) that intercepts property assignments to execute custom callback logic. It abstracts the boilerplate of custom setters by routing property mutations through a delegated `ReadWriteProperty` instance.

The Kotlin standard library provides two primary functions for observable delegation: `Delegates.observable` and `Delegates.vetoable`. Both functions take an initial value and a handler lambda, returning an `ObservableProperty<T>` that manages the property's backing field.

## `Delegates.observable`

The `observable` function executes its callback **after** the assignment has been made to the backing field.

**Syntax:**

```kotlin theme={"dark"}
inline fun <T> observable(
    initialValue: T,
    crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit
): ReadWriteProperty<Any?, T>
```

**Implementation:**

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

class StateManager {
    var status: String by Delegates.observable("IDLE") { property, oldValue, newValue ->
        // This block executes immediately after the backing field is updated.
        println("${property.name} transitioned: $oldValue -> $newValue")
    }
}
```

## `Delegates.vetoable`

The `vetoable` function executes its callback **before** the assignment is committed to the backing field. The lambda must return a `Boolean`. If the lambda evaluates to `true`, the mutation proceeds. If it evaluates to `false`, the assignment is intercepted and discarded, leaving the backing field unmodified.

**Syntax:**

```kotlin theme={"dark"}
inline fun <T> vetoable(
    initialValue: T,
    crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Boolean
): ReadWriteProperty<Any?, T>
```

**Implementation:**

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

class Configuration {
    var threshold: Int by Delegates.vetoable(10) { property, oldValue, newValue ->
        // The assignment only succeeds if this expression evaluates to true.
        newValue >= oldValue 
    }
}
```

## Internal Mechanics

When you use the `by` keyword with these delegates, the Kotlin compiler generates a hidden reference to the `ObservableProperty` object and routes all `getValue` and `setValue` calls through it.

1. **`KProperty<*>`:** The first parameter of the callback provides reflection-based metadata about the delegated property. It is most commonly used to access `property.name`.
2. **`ObservableProperty<T>`:** Both `observable` and `vetoable` instantiate an anonymous subclass of `ObservableProperty<T>`.
   * In `observable`, the subclass overrides the `afterChange` method. The base `setValue` implementation updates the internal value, then invokes `afterChange`.
   * In `vetoable`, the subclass overrides the `beforeChange` method. The base `setValue` implementation invokes `beforeChange`; it only updates the internal value if `beforeChange` returns `true`.
3. **Thread Safety:** Neither `Delegates.observable` nor `Delegates.vetoable` are inherently thread-safe. If the delegated property is mutated concurrently from multiple threads, the callbacks and the backing field updates are subject to race conditions unless externally synchronized.

<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>
