Show all four coalescing comparison operators on Integer values.

← Operators and Expressions · Ref: Q1227

EK9 has four coalescing comparison operators. All handle unset values gracefully.

  minOf() as pure
    -> left as Integer, right as Integer
    <- rtn as Integer: left <? right     //lesser
  maxOf() as pure
    -> left as Integer, right as Integer
    <- rtn as Integer: left >? right     //greater
  minOrEqual() as pure
    -> left as Integer, right as Integer
    <- rtn as Integer: left <=? right    //lesser-or-equal
  maxOrEqual() as pure
    -> left as Integer, right as Integer
    <- rtn as Integer: left >=? right    //greater-or-equal

All four are COMPARISON operators — they compare two values and return a result. They are NOT assignment operators. Do not confuse them with :=? (guarded assignment), which is a completely different operation.

See Q1226 for the distinction between <? and :=?. See Q1092 for min/max coalescing basics.

Example

defines module qa.operators.allcoalescing

  defines function

    // <? — returns the LESSER of two values
    minOf() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left <? right

    // >? — returns the GREATER of two values
    maxOf() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left >? right

    // <=? — returns the lesser-or-equal of two values
    minOrEqual() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left <=? right

    // >=? — returns the greater-or-equal of two values
    maxOrEqual() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left >=? right

  defines program

    AllCoalescingOperatorsDemo()
      stdout <- Stdout()

      // === ALL FOUR OPERATORS WITH BOTH VALUES SET ===

      scoreA <- 78
      scoreB <- 92

      minResult <- minOf(scoreA, scoreB)
      stdout.println(`<? minimum: ${minResult}`)

      maxResult <- maxOf(scoreA, scoreB)
      stdout.println(`>? maximum: ${maxResult}`)

      lessOrEqualResult <- minOrEqual(scoreA, scoreB)
      stdout.println(`<=? lesser-or-equal: ${lessOrEqualResult}`)

      greaterOrEqualResult <- maxOrEqual(scoreA, scoreB)
      stdout.println(`>=? greater-or-equal: ${greaterOrEqualResult}`)

      // === WITH EQUAL VALUES ===

      valueA <- 50
      valueB <- 50

      equalMin <- minOf(valueA, valueB)
      stdout.println(`<? with equal values: ${equalMin}`)

      equalMax <- maxOf(valueA, valueB)
      stdout.println(`>? with equal values: ${equalMax}`)

      // === WITH ONE UNSET — all return the set value ===

      activeReading <- 65
      offlineReading <- Integer()

      minUnset <- minOf(activeReading, offlineReading)
      stdout.println(`<? one unset: ${minUnset}`)

      maxUnset <- maxOf(activeReading, offlineReading)
      stdout.println(`>? one unset: ${maxUnset}`)

      lessEqualWhenUnset <- minOrEqual(activeReading, offlineReading)
      stdout.println(`<=? one unset: ${lessEqualWhenUnset}`)

      greaterEqualWhenUnset <- maxOrEqual(activeReading, offlineReading)
      stdout.println(`>=? one unset: ${greaterEqualWhenUnset}`)

      // === WITH BOTH UNSET — all return unset ===

      missingA <- Integer()
      missingB <- Integer()

      noneMin <- minOf(missingA, missingB)
      if noneMin?
        stdout.println(`Unexpected: ${noneMin}`)
      else
        stdout.println("<? both unset: result is unset")
Other ways to ask this
  • What are all the coalescing operators in EK9 and how do they differ?
  • I need to see <?, >?, <=?, >=? side by side on the same type.
  • In Java there's no equivalent to coalescing operators — show the full EK9 family.
  • Demonstrate the complete set of coalescing comparison operators with Integer examples.

Coming from another language?

Java: requires 4 separate utility methods with null checks. Python: min/max handle None only with filtering. Kotlin: requires custom extension functions. EK9: <?, >?, <=?, >=? — four built-in operators that handle unset values automatically.

Keywords: integer, >?, family, coalescing, <=?, <?, comparison, all, >=?