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

The `--` operator in Kotlin is the unary decrement operator, used to subtract one from the value of a mutable variable. Under the hood, it acts as syntactic sugar for the `dec()` operator function, meaning the expression `a--` or `--a` is translated by the compiler to `a = a.dec()`.

The operator can be applied in two distinct positions relative to the operand, which dictates the evaluation order when used within a larger expression:

1. **Prefix (`--a`)**: The decrement operation is evaluated *before* the expression resolves. It subtracts one from the variable and returns the newly decremented value.
2. **Postfix (`a--`)**: The decrement operation is evaluated *after* the expression resolves. It returns the original value of the variable, and then subtracts one from the underlying variable in memory.

```kotlin theme={"dark"}
var x = 5
val y = --x // Prefix: x is decremented to 4, then y is assigned 4

var a = 5
val b = a-- // Postfix: b is assigned 5, then a is decremented to 4
```

## Operator Overloading and Custom Types

Because Kotlin utilizes an operator overloading convention, the `--` operator is not restricted to built-in numeric primitives. It can be applied to any custom class, provided the following conditions are met:

1. The variable being decremented is mutable (declared as `var`), because the operator performs a reassignment.
2. The type implements a `dec()` member function or extension function.
3. The `dec()` function is prefixed with the `operator` keyword.
4. The `dec()` function returns a value of the same type (or a subtype) as the caller.

```kotlin theme={"dark"}
class Counter(val value: Int) {
    // Defines the behavior for the '--' operator
    operator fun dec(): Counter {
        return Counter(value - 1)
    }
}

var myCounter = Counter(10)
myCounter-- // Compiler translates this to: myCounter = myCounter.dec()
```

## Thread Safety

The `--` operator is **not** atomic. The translation to `a = a.dec()` involves three distinct steps: reading the current value, invoking the decrement logic, and writing the new value back to the variable. In a multithreaded environment, concurrent use of the `--` operator on shared variables requires explicit synchronization or the use of atomic classes (e.g., `AtomicInteger.decrementAndGet()`).

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