> ## 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 Unary Minus

Operators in Kotlin are special symbols or keywords that perform specific operations on one or more operands. Architecturally, Kotlin implements most operators as syntactic sugar for standard method calls on objects. This mechanism is governed by the `operator` modifier, which allows classes to define or overload operator behavior by providing specifically named member or extension functions.

## Operator Overloading Mechanism

When the compiler encounters an operator, it resolves it to a corresponding function call. For example, the expression `a + b` is translated to `a.plus(b)`. To enable this for custom types, the function must be prefixed with the `operator` keyword.

```kotlin theme={"dark"}
class Vector(val x: Int, val y: Int) {
    operator fun plus(other: Vector): Vector {
        return Vector(this.x + other.x, this.y + other.y)
    }
}
```

## Operator Categories and Function Mapping

### Unary Prefix Operators

These operate on a single operand and are evaluated before the expression.

| Expression | Translated Method Call |
| :--------- | :--------------------- |
| `+a`       | `a.unaryPlus()`        |
| `-a`       | `a.unaryMinus()`       |
| `!a`       | `a.not()`              |

### Increments and Decrements

These operators mutate the operand. The compiler handles the assignment automatically; the overloaded function must return the new value, not mutate the object in place.

| Expression    | Translated Method Call |
| :------------ | :--------------------- |
| `a++` / `++a` | `a.inc()`              |
| `a--` / `--a` | `a.dec()`              |

*Note: Prefix forms (`++a`) return the updated value. Postfix forms (`a++`) store the initial value, perform the `inc()` operation, assign the result to `a`, and return the stored initial value.*

### Arithmetic Operators

Binary operators that perform standard mathematical computations.

| Expression | Translated Method Call |
| :--------- | :--------------------- |
| `a + b`    | `a.plus(b)`            |
| `a - b`    | `a.minus(b)`           |
| `a * b`    | `a.times(b)`           |
| `a / b`    | `a.div(b)`             |
| `a % b`    | `a.rem(b)`             |

### Augmented Assignments

These combine an arithmetic operation with assignment. The compiler resolves expressions like `a += b` based on the availability of specific assignment functions (e.g., `plusAssign`) versus standard arithmetic functions (e.g., `plus`).

* If `plusAssign` is defined, it translates to `a.plusAssign(b)`.
* If `plusAssign` is not defined, but `plus` is defined and `a` is a mutable variable (`var`), it translates to `a = a.plus(b)`.
* If **both** are defined and `a` is mutable, the compiler reports an `Assignment operators ambiguity` error to prevent unpredictable mutation behavior.
  \| Expression | Translated Method Call (if specific assignment exists) |
  \| :--- | :--- |
  \| `a += b` | `a.plusAssign(b)` |
  \| `a -= b` | `a.minusAssign(b)` |
  \| `a *= b` | `a.timesAssign(b)` |
  \| `a /= b` | `a.divAssign(b)` |
  \| `a %= b` | `a.remAssign(b)` |

### Range Operators

These operators create ranges or progressions between two values.

| Expression | Translated Method Call |
| :--------- | :--------------------- |
| `a..b`     | `a.rangeTo(b)`         |
| `a..<b`    | `a.rangeUntil(b)`      |

### Equality and Inequality

Kotlin distinguishes between structural equality (`==`) and referential equality (`===`).

**Structural Equality:**
Translated to the `equals()` function. The compiler injects null-safety checks during translation.

| Expression | Translated Method Call            |
| :--------- | :-------------------------------- |
| `a == b`   | `a?.equals(b) ?: (b === null)`    |
| `a != b`   | `!(a?.equals(b) ?: (b === null))` |

**Referential Equality:**
Evaluates whether two references point to the exact same memory address. These operators (`===` and `!==`) are intrinsic to the JVM/runtime and **cannot** be overloaded.

### Comparison Operators

Translated to the `compareTo` method, which must return an `Int` adhering to the standard contract (negative if less, zero if equal, positive if greater).

| Expression | Translated Method Call |
| :--------- | :--------------------- |
| `a > b`    | `a.compareTo(b) > 0`   |
| `a < b`    | `a.compareTo(b) < 0`   |
| `a >= b`   | `a.compareTo(b) >= 0`  |
| `a <= b`   | `a.compareTo(b) <= 0`  |

### Collection and Access Operators

Kotlin provides operators for membership checking and indexed access.

| Expression | Translated Method Call |
| :--------- | :--------------------- |
| `a in b`   | `b.contains(a)`        |
| `a !in b`  | `!b.contains(a)`       |
| `a[i]`     | `a.get(i)`             |
| `a[i, j]`  | `a.get(i, j)`          |
| `a[i] = b` | `a.set(i, b)`          |

### Invoke Operator

Parentheses translate to the `invoke` function, allowing instances to be called as if they were functions.

| Expression | Translated Method Call |
| :--------- | :--------------------- |
| `a()`      | `a.invoke()`           |
| `a(i)`     | `a.invoke(i)`          |

### Destructuring Declaration Operators

Kotlin allows unpacking an object into multiple variables. This syntax is powered by sequentially numbered `component` functions.

| Expression       | Translated Method Call                                 |
| :--------------- | :----------------------------------------------------- |
| `val (x, y) = a` | `val x = a.component1()`<br />`val y = a.component2()` |

### Iterator Operator

For an object to be iterable within a `for` loop, it must provide an `iterator` operator function. The returned iterator object must subsequently provide `next()` and `hasNext()` operator functions.

| Expression        | Translated Method Call                                                       |
| :---------------- | :--------------------------------------------------------------------------- |
| `for (item in a)` | `val it = a.iterator()`<br />`while (it.hasNext()) { val item = it.next() }` |

### Property Delegation Operators

The `by` keyword delegates the getter (and setter) of a property to another object. The delegate object must provide `getValue` and, for mutable properties, `setValue` operator functions. An optional `provideDelegate` operator can also be defined to intercept the delegation creation.

| Expression                    | Translated Method Call                                                      |
| :---------------------------- | :-------------------------------------------------------------------------- |
| `val p by d`                  | `d.getValue(thisRef, property)`                                             |
| `var p by d`<br />`p = value` | `d.getValue(thisRef, property)`<br />`d.setValue(thisRef, property, value)` |

## Bitwise Operations

Unlike C-style languages, Kotlin does not use symbolic operators (like `&`, `|`, `<<`) for bitwise operations. Instead, it utilizes named functions combined with the `infix` modifier, allowing them to be called without dot notation or parentheses.

```kotlin theme={"dark"}
val leftShift = 1 shl 2
val bitwiseAnd = 0x0F and 0xF0
```

Standard bitwise functions include: `shl` (signed shift left), `shr` (signed shift right), `ushr` (unsigned shift right), `and`, `or`, `xor`, and `inv` (bitwise inversion, called as a standard method).

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