> ## 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 Pre-Increment

The `++` operator in Kotlin is a unary arithmetic operator used to increment the value of an assignable expression. Under the hood, it acts as syntactic sugar for the `inc()` operator function. Kotlin guarantees strict single-evaluation for the receiver, meaning the expression being incremented is evaluated exactly once, even if it involves custom getters or indexed access.

## Evaluation Strategies

The operator has two distinct syntactic forms that dictate the order of evaluation and the returned value. To prevent evaluating the receiver multiple times, the compiler utilizes temporary variables during resolution.

**1. Prefix (`++a`)**
The prefix form increments the expression and returns the newly incremented value.

* **Resolution:**
  1. Evaluate the receiver `a` once and store its current value in a temporary variable (`temp = a`).
  2. Call `inc()` on the temporary variable and assign the result back to `a` (`a = temp.inc()`).
  3. Return the new value of `a`.

```kotlin theme={"dark"}
var a = 5
val b = ++a // a becomes 6, b is assigned 6
```

**2. Postfix (`a++`)**
The postfix form returns the original value of the expression before the increment occurs.

* **Resolution:**
  1. Evaluate the receiver `a` once and store its current value in a temporary variable (`temp = a`).
  2. Call `inc()` on the temporary variable and assign the result back to `a` (`a = temp.inc()`).
  3. Return the original cached value (`temp`).

```kotlin theme={"dark"}
var x = 5
val y = x++ // y is assigned 5, x becomes 6
```

## Operator Overloading

To utilize the `++` operator with custom types, the class must define an `inc()` method prefixed with the `operator` modifier. Because the `++` operator inherently performs a reassignment, the `inc()` function must return a new instance (or the mutated instance) that is assignable back to the original expression.

```kotlin theme={"dark"}
class Counter(val value: Int) {
    operator fun inc(): Counter {
        return Counter(value + 1)
    }
}

var myCounter = Counter(10)
myCounter++ // Resolves using the single-evaluation strategy: temp = myCounter; myCounter = temp.inc()
```

## Compiler Requirements

* **Assignable Expression:** The operand must be an assignable expression. While this includes mutable variables (`var`), it also encompasses mutable properties and indexed access expressions (e.g., `array[0]++` or `list[i]++`), even if the array or list reference itself is declared as a read-only `val`.
* **Type Compatibility:** The return type of the `inc()` function must be the same type as the original expression, or a subtype, to satisfy the implicit `a = temp.inc()` reassignment step.

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