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

The `+` operator in Kotlin is a statically resolved, overloadable operator. At compile time, the Kotlin compiler translates the `+` expression into a direct method call to a corresponding function invoked on the left-hand operand (the receiver), passing the right-hand operand as the argument.

## Lexical Translation

The compiler maps the `+` token to specific function names depending on its arity (unary or binary):

* **Binary operation:** `a + b` is translated to `a.plus(b)`
* **Unary operation:** `+a` is translated to `a.unaryPlus()`

```kotlin theme={"dark"}
val a = 5
val b = 10

// Binary translation
val x = a + b
val y = a.plus(b) // Exact bytecode equivalent

// Unary translation
val i = +a
val j = a.unaryPlus() // Exact bytecode equivalent
```

## Operator Overloading Mechanics

Kotlin allows custom types to define their own behavior for the `+` operator. To enable this, a class or an extension function must declare a function named exactly `plus` (or `unaryPlus`) and prefix it with the `operator` modifier. Without the `operator` keyword, the compiler will not map the `+` symbol to the function.

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

    // Overloading the unary '+' operator
    // By mathematical convention, unary plus is an identity operation
    operator fun unaryPlus(): Vector {
        return Vector(+this.x, +this.y)
    }
}
```

## Type Resolution and Signatures

The resolution of the `+` operator is strictly determined by the static type of the receiver. The right-hand operand does not need to match the type of the left-hand operand, provided a matching `plus()` signature exists. Furthermore, the return type is entirely dictated by the function signature.

```kotlin theme={"dark"}
class Matrix {
    // The right-hand operand is a Scalar, but the return type is a Matrix
    operator fun plus(scalar: Int): Matrix {
        TODO("Matrix addition logic")
    }
}

// Usage:
val result: Matrix = Matrix() + 5 
```

## Immutability Convention

By convention and standard library design, the `+` operator is non-mutating. It does not alter the state of either the receiver or the argument. Instead, it allocates and returns a new object representing the combined result.

## Standard Library Implementations

The Kotlin standard library provides built-in `operator fun plus` implementations for several core types:

* **Primitives:** Mapped to JVM-level arithmetic addition instructions (e.g., `IADD`, `DADD`).
* **Strings:** On JVM 9+ targets (since Kotlin 1.5.20), string concatenation is compiled using `invokedynamic` (specifically `StringConcatFactory`) to optimize performance and memory allocation.
* **Collections:** Implemented as extension functions that allocate a new read-only collection. This new collection contains the elements from the left operand followed by the right operand, where the right operand can be either a single element or another collection.

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