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

The `--` operator in Kotlin is the unary decrement operator. It decreases the value of its operand by one. At compile time, Kotlin translates the `--` operator into a call to the `dec()` operator function, followed by a reassignment to the original operand.

The operator operates in two distinct evaluation modes depending on its position relative to the operand:

**Prefix Decrement (`--a`)**
The operand is decremented first, and the expression evaluates to the *new* decremented value.

**Postfix Decrement (`a--`)**
The expression evaluates to the *original* value of the operand, and the operand is decremented immediately afterward.

```kotlin theme={"dark"}
var prefixVar = 10
val prefixResult = --prefixVar 
// prefixVar is now 9
// prefixResult is 9

var postfixVar = 10
val postfixResult = postfixVar-- 
// postfixVar is now 9
// postfixResult is 10
```

## Compiler Translation

Because Kotlin supports operator overloading, the `--` operator is not limited to primitive numeric types. When the compiler encounters `--a` or `a--`, it resolves the operation to specific underlying execution steps to preserve the correct evaluation state.

**Prefix Translation (`--a`)**

```kotlin theme={"dark"}
a = a.dec()
// The expression evaluates to the new value of 'a'
```

**Postfix Translation (`a--`)**

```kotlin theme={"dark"}
val a0 = a       // Cache the original value
a = a0.dec()     // Reassign the decremented value
// The expression evaluates to 'a0'
```

## Operator Overloading for Custom Types

To enable the `--` operator on a custom class, you must define an `operator fun dec()` that returns a value of the same type (or a subtype). The compiler automatically handles the reassignment of the returned value back to the operand.

```kotlin theme={"dark"}
class Vector(val x: Int, val y: Int) {
    // The dec() function must be marked with the 'operator' modifier
    operator fun dec(): Vector {
        return Vector(x - 1, y - 1)
    }
}

var v = Vector(5, 5)
v-- 
```

**Technical Constraints:**

* The operand must be an expression that is valid on the left-hand side of an assignment. This includes mutable local variables (`var`), mutable properties of objects (`obj.property--`), and indexed access expressions where the `get` operator returns a non-nullable type (e.g., `array[0]--`).
* The `dec()` function must return a value. It should not mutate the object in place and return `Unit`, as the compiler explicitly reassigns the result of `dec()` to the original operand reference.
* Nullable types do not possess a `dec()` operator. Consequently, applying `--` to an indexed access expression that returns a nullable type (such as `map[key]--` on a standard `MutableMap`) will result in a compilation error.

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