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

The `+` operator in Kotlin is a statically resolved, overloadable operator primarily functioning as a binary addition/concatenation operator or a unary positive operator. Rather than being a hardcoded language construct limited to primitives, the Kotlin compiler translates the `+` symbol into explicit method calls to functions named `plus` (for binary operations) or `unaryPlus` (for unary operations).

## Compiler Translation Mechanics

When the Kotlin compiler encounters the `+` operator, it performs a desugaring process based on the arity of the operation.

**Binary Operation (`a + b`)**
The compiler translates the infix `+` operator into a call to the `plus()` function invoked on the left-hand operand, passing the right-hand operand as the argument.

```kotlin theme={"dark"}
val result = a + b
// Compiles down to:
val result = a.plus(b)
```

**Unary Operation (`+a`)**
When used as a prefix, the compiler translates the `+` operator into a call to the `unaryPlus()` function invoked on the operand.

```kotlin theme={"dark"}
val result = +a
// Compiles down to:
val result = a.unaryPlus()
```

## Type-Specific Implementations

Because `+` relies on function resolution, its behavior is strictly dictated by the type of the left-hand operand:

* **Primitives:** For numeric types (`Int`, `Double`, etc.), Kotlin maps the `plus` and `unaryPlus` functions directly to the corresponding JVM bytecode instructions (e.g., `iadd`, `dadd`) to ensure zero runtime overhead.
* **Strings:** When the left operand is a `String`, `plus(Any?)` is invoked. On the JVM, Kotlin optimizes this during compilation using `StringBuilder` or `StringConcatFactory` (depending on the target JVM version) rather than executing literal method calls.
* **Collections:** The Kotlin standard library provides extension functions for `Iterable.plus` and `Collection.plus`. Mechanically, this operator does not mutate the existing collection; it allocates and returns a *new* read-only collection containing the elements of the original collection followed by the appended element(s).

## Operator Overloading Syntax

To enable the `+` operator for custom types, you must declare a member or extension function using the `operator` modifier. The function must be named exactly `plus` or `unaryPlus`.

```kotlin theme={"dark"}
class Matrix(val data: List<Int>) {
    // Overloading the binary '+' operator
    operator fun plus(other: Matrix): Matrix {
        require(this.data.size == other.data.size)
        return Matrix(this.data.zip(other.data) { a, b -> a + b })
    }

    // Overloading the unary '+' operator
    operator fun unaryPlus(): Matrix {
        return Matrix(this.data.map { +it })
    }
}
```

## Resolution Rules and Constraints

1. **Asymmetry:** The `plus` function does not require the parameter type to match the receiver type. `a + b` is valid as long as `a` has a `plus` function accepting the type of `b`.
2. **Return Type:** The return type of the `plus` or `unaryPlus` function dictates the type of the resulting expression. It is not constrained to return the same type as the operands.
3. **Precedence:** The binary `+` operator shares the same precedence level as the binary `-` operator, which is lower than multiplicative operators (`*`, `/`, `%`) but higher than infix functions and comparison operators. Unary `+` has a higher precedence than all binary arithmetic operators.
4. **Nullability:** The `+` operator can be invoked where the left operand is a nullable variable (written simply as `a + b` where `a` is of type `T?`) only if an explicit extension function for the nullable type is defined and resolved (e.g., `operator fun Point?.plus(other: Point): Point`). Without such an extension, the compiler enforces null safety and rejects the operation.

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