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 is the open-ended range operator in Kotlin, used to construct a range of values that includes the lower bound but strictly excludes the upper bound. It provides a mathematically intuitive representation of half-open intervals [a,b)[a, b) and serves as the native syntactic replacement for the until infix function.

Syntax and Compilation

The operator is placed between the start and end bounds:
val range = start ..< end
During compilation, the Kotlin compiler desugars the ..< operator into a method call to the rangeUntil operator function. The above syntax is strictly equivalent to:
val range = start.rangeUntil(end)

Type System Integration

The behavior and return type of the ..< operator depend on the operand types:
  1. Integral Types (Int, Long, Short, Byte, Char): For discrete integral types, the operator returns a standard Iterable progression (e.g., IntRange, LongRange). The standard library implementation of rangeUntil for these types explicitly guards against integer underflow. Rather than blindly calculating end - 1 to find an inclusive upper bound, the function checks the exclusive bound against the type’s minimum value (e.g., if (to <= Int.MIN_VALUE) return IntRange.EMPTY). This ensures that an expression like 0 ..< Int.MIN_VALUE safely returns an empty range instead of underflowing to Int.MAX_VALUE.
val intRange: IntRange = 0 ..< 10 val emptyRange: IntRange = 0 ..< Int.MIN_VALUE // Safely returns IntRange.EMPTY
2. **Floating-Point Types (`Float`, `Double`)**:

   Because `Float` and `Double` implement the `Comparable` interface, they automatically inherit the generic `Comparable<T>.rangeUntil` extension function from the Kotlin standard library. Expressions using `..<` with these types compile successfully and return an `OpenEndRange<Float>` or `OpenEndRange<Double>`.
   ```kotlin
val doubleRange: OpenEndRange<Double> = 1.0 ..< 2.0

Operator Overloading and Custom Types

Any class that implements the Comparable<T> interface automatically supports the ..< operator without requiring a manual implementation of the rangeUntil member function. The standard library provides the extension operator fun <T : Comparable<T>> T.rangeUntil(that: T): OpenEndRange<T>, which handles the boundary evaluation.
class Version(val major: Int, val minor: Int) : Comparable<Version> {
    override fun compareTo(other: Version): Int {
        return compareValuesBy(this, other, { it.major }, { it.minor })
    }
}

// Syntax visualization: Automatically supported via Comparable<T>.rangeUntil
val v1 = Version(1, 0)
val v2 = Version(2, 0)
val versionRange: OpenEndRange<Version> = v1 ..< v2
Explicit implementation of the rangeUntil operator function is only required for non-comparable types, or when a custom type needs to return a specialized discrete progression (such as a custom Iterable range) rather than the default continuous OpenEndRange<T>.

Behavioral Characteristics

  • Ascending Order Only: The ..< operator strictly creates ascending ranges. If the left operand is greater than or equal to the right operand, the resulting range is empty (isEmpty() returns true).
  • Step Size: For integral progressions, the default step size is 1.
  • Interface Hierarchy: The OpenEndRange<T> interface is distinct from ClosedRange<T> (which is generated by the .. operator). OpenEndRange exposes an endExclusive property, whereas ClosedRange exposes an endInclusive property.
Master Kotlin with Deep Grasping Methodology!Learn More