> ## 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 Less Than

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

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

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