How does the >? coalescing maximum operator work in EK9?

← Operators and Expressions · Ref: Q964

The >? operator is a COALESCING MAXIMUM. It returns the larger of two SET values. If either value is UNSET, the result depends on which is set:

- Both SET: returns the larger value (like Math.max)
- Left SET, right UNSET: returns left (the SET one)
- Left UNSET, right SET: returns right (the SET one)
- Both UNSET: returns UNSET

This is unique to EK9 — no other language has coalescing comparators.

The <=? and >=? operators also exist for 'less-or-equal minimum' and 'greater-or-equal maximum'.

See Q899 for the full coalescing operator family. See Q963 for <? coalescing minimum.

Example

defines module qa.operators.coalescingmax

  defines function

    <?-
      Find the highest temperature using >? coalescing maximum.
      If either reading is unset, returns the other.
    -?>
    peakTemperature() as pure
      ->
        morningReading as Float
        afternoonReading as Float
      <-
        rtn as Float: morningReading >? afternoonReading

    maxOf() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left >? right

    minOrEqual() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left <=? right

    maxOrEqual() as pure
      ->
        left as Integer
        right as Integer
      <- rtn as Integer: left >=? right

  defines program

    CoalescingMaxDemo()
      stdout <- Stdout()

      // Both values set — returns the larger
      highest <- maxOf(85, 92)
      stdout.println(`Highest: ${highest}`)

      // One value unset — returns the SET one
      knownScore <- 75
      missingScore <- Integer()
      safeScore <- knownScore >? missingScore
      stdout.println(`Safe score: ${safeScore}`)

      // Both unset — result is unset
      noScore1 <- Integer()
      noScore2 <- Integer()
      noResult <- noScore1 >? noScore2
      if noResult?
        stdout.println("Should not reach here")
      else
        stdout.println("Both unset, result is unset")

      // <=? coalescing less-or-equal minimum
      minimum <- minOrEqual(10, 10)
      stdout.println(`<=? with equal: ${minimum}`)

      // >=? coalescing greater-or-equal maximum
      maximum <- maxOrEqual(10, 10)
      stdout.println(`>=? with equal: ${maximum}`)

      // Practical use: find peak temperature across readings
      peak <- peakTemperature(22.5, 31.8)
      stdout.println(`Peak temp: ${peak}`)

Common mistakes

E50001 — EK9 has no max() function so 'max' will not resolve (E50001); use the >? coalescing maximum operator instead. See ek9 -h E50001 for details.

Incorrect:

highest <- max(85, 92)

Correct:

highest <- maxOf(85, 92)
Other ways to ask this
  • What does >? do in EK9?
  • How do I find the maximum of two values safely in EK9?
  • What is EK9's safe maximum operator?
  • How does coalescing greater-than work?

Coming from another language?

Java: Math.max(a, b) requires null checks. Python: max(a, b) throws on None. EK9's >? combines comparison + unset handling in one operator. No equivalent exists in any mainstream language.

Keywords: unset handling, coalescing, safe comparison, maximum, greater than