Skip to main content

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.

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()
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.
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.
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.
Master Kotlin with Deep Grasping Methodology!Learn More