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 or equal to) operator is a relational operator that evaluates whether the left operand is less than or equal to the right operand, based on their natural or custom semantic ordering. For object types, this operator acts as syntactic sugar that delegates to the compareTo method. However, for built-in primitive types (such as Int, Float, and Double), the Kotlin compiler explicitly optimizes the operator down to native, hardcoded primitive bytecode instructions to avoid function call and boxing overhead. When the Kotlin compiler encounters the <= operator on object types, it translates the expression into a method call comparing the returned integer against zero.
// Source expression
a <= b

// Compiler translation (for object types)
a.compareTo(b) <= 0

Type Constraints and Comparable

For the <= operator to compile on non-primitive types, the left operand (a) must provide an operator fun compareTo that accepts the right operand (b) as its argument. This is typically achieved by implementing the Comparable<T> interface. The compareTo function must return an Int adhering to the following contract:
  • Negative integer (< 0): The receiver object is less than the argument.
  • Zero (== 0): The receiver object is equal to the argument.
  • Positive integer (> 0): The receiver object is greater than the argument.
Because <= checks if the result is <= 0, it evaluates to true for both the negative integer and zero conditions.

Operator Overloading

You can enable the <= operator for custom types by overriding the compareTo function and marking it with the operator modifier. If the class implements Comparable<T>, the operator modifier is implicitly inherited from the interface’s compareTo function and does not need to be explicitly declared.
class Metric(val value: Int) : Comparable<Metric> {
    // The 'operator' modifier is implicitly inherited from Comparable<T>
    override fun compareTo(other: Metric): Int {
        return this.value.compareTo(other.value)
    }
}
Alternatively, the operator can be provided via an extension function if modifying the original class is not possible:
operator fun ExternalType.compareTo(other: ExternalType): Int {
    return this.internalValue.compareTo(other.internalValue)
}

Null Safety Mechanics

The <= operator does not inherently handle nullable types. If a nullable type (T?) is used, compilation fails for one of two distinct reasons depending on the operand’s position:
  • Left operand is nullable: The compiler rejects the expression because the compareTo function cannot be safely invoked on a potentially null receiver.
  • Right operand is nullable: The compiler rejects the expression due to a parameter type mismatch, as the standard compareTo function expects a strictly non-null argument.
To use <= with nullable types, you must explicitly unwrap the values or provide a custom compareTo extension function that accepts nullable receivers or arguments.

Floating-Point Resolution

When using <= with statically typed Float or Double operands, Kotlin uses the IEEE 754 standard for floating-point comparison. However, if the operands are boxed—such as when typed as Comparable<Double>, Comparable<Float>, Number, Any, or their nullable counterparts (e.g., Double?)—Kotlin falls back to the compareTo implementation of the wrapper classes. This creates a strict mechanical distinction:
  • IEEE 754 (Float / Double): NaN <= NaN evaluates to false. 0.0 <= -0.0 evaluates to true (because 0.0 and -0.0 are considered equal).
  • Boxed Comparable: NaN <= NaN evaluates to true (NaN is considered equal to itself and greater than any other value). 0.0 <= -0.0 evaluates to false (because 0.0 is considered strictly greater than -0.0).
Master Kotlin with Deep Grasping Methodology!Learn More