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 < (less than) operator in Kotlin is a relational operator used to evaluate the strict lower bound of one operand relative to another. Rather than being a strictly primitive language construct, Kotlin implements < via operator overloading and compile-time desugaring. When the Kotlin compiler encounters the < operator, it translates the expression into a method call to the compareTo function. Specifically, the expression a < b is evaluated as a.compareTo(b) < 0.
val a = 5
val b = 10

// Standard operator syntax
val isLessStandard = a < b

// Compile-time desugared equivalent
val isLessDesugared = a.compareTo(b) < 0

The compareTo Contract

To support the < operator, the left-hand operand’s type must define a compareTo function that accepts the right-hand operand’s type and returns an Int. This is typically achieved by having the class implement the Comparable<T> interface. The returned Int dictates the relational state:
  • A negative integer (< 0) indicates the left operand is less than the right operand.
  • Zero (0) indicates equality in terms of ordering.
  • A positive integer (> 0) indicates the left operand is greater than the right operand.

Custom Implementation Syntax

To enable the < operator for custom types, the type must provide a compareTo function. The function must be marked with the operator keyword for the compiler to bind it to the < symbol. However, if the class implements the Comparable<T> interface, the operator modifier is inherited automatically and should not be explicitly declared.
class Magnitude(val scalar: Int) : Comparable<Magnitude> {
    // The 'operator' modifier is inherited from Comparable<Magnitude>,
    // which implicitly binds the < operator to this function.
    override fun compareTo(other: Magnitude): Int {
        return this.scalar.compareTo(other.scalar)
    }
}

class CustomValue(val value: Int) {
    // When not implementing Comparable, the 'operator' keyword 
    // is strictly required to bind the < operator.
    operator fun compareTo(other: CustomValue): Int {
        return this.value.compareTo(other.value)
    }
}

Type Constraints and Nullability

  • Type Safety: The < operator is statically typed. The compiler enforces that the right-hand operand matches the parameter type defined in the left-hand operand’s compareTo signature.
  • Nullability: The standard < operator cannot be invoked directly on nullable types (T?). Operands must be non-null, or explicitly unwrapped, because the desugared compareTo call requires a non-null receiver.
  • Primitive Optimization: For primitive types like Int, Float, and Double, the < operator does not incur the overhead of a method call. The compiler optimizes the expression directly into the corresponding JVM bytecode comparison instructions (e.g., if_icmplt, fcmpg), adhering to IEEE 754 standards for floating-point numbers.
Master Kotlin with Deep Grasping Methodology!Learn More