Get the higher temperature threshold, and which one wins when two are equal?

← Operators and Expressions · Ref: Q1219

The >? coalescing operator returns the greater of two values, handling unset gracefully.

  higherThreshold() as pure
    ->
      left as Float
      right as Float
    <- rtn as Float: left >? right

If both values are set and different, >? returns the larger one. If one is unset, it returns the set one. If both unset, the result is unset.

When the two values are equal (<=> gives 0), >? returns the LEFT operand, exactly as <? does. For Float values equal means identical, so you cannot tell; it matters for objects that <=> ranks equal but that differ. To get the right operand on a tie, swap the operands. There is no separate 'or equal' form - the inclusive operators were removed (E01095). See Q1227 for both operators side by side.

Do not confuse any coalescing operator with :=? (assign-if-unset). See Q1226 for the contrast.

Example

#!ek9
defines module qa.operators.coalescetie.float

  defines function

    higherThreshold() as pure
      ->
        left as Float
        right as Float
      <- rtn as Float: left >? right

  defines program

    CoalesceTieFloatDemo()
      stdout <- Stdout()

      // === BOTH THRESHOLDS SET — returns the higher ===

      warningThreshold <- 35.0
      criticalThreshold <- 40.0
      upperLimit <- higherThreshold(warningThreshold, criticalThreshold)
      stdout.println(`Upper threshold: ${upperLimit}`)

      // === EQUAL THRESHOLDS — returns the shared value (the left operand) ===

      limitA <- 37.5
      limitB <- 37.5
      sameLimit <- higherThreshold(limitA, limitB)
      stdout.println(`Shared threshold: ${sameLimit}`)

      // === ONE THRESHOLD UNSET — returns the set one ===

      configuredLimit <- 42.0
      missingLimit <- Float()
      onlyLimit <- higherThreshold(configuredLimit, missingLimit)
      stdout.println(`Configured threshold: ${onlyLimit}`)

      // === BOTH UNSET — result is unset ===

      absentFirst <- Float()
      absentSecond <- Float()
      bothMissing <- higherThreshold(absentFirst, absentSecond)
      if bothMissing?
        stdout.println(`Threshold: ${bothMissing}`)
      else
        stdout.println("No temperature threshold configured")
Other ways to ask this
  • How do I find the greater of two Float values when one threshold might be unset?
  • Two temperature thresholds are defined but one might be missing — pick the higher.
  • In Java I'd need null checks before Math.max with nullable doubles. What does EK9 use?
  • Migrating from Go where I compare float64 with nil checks — does EK9 have a null-safe maximum, and what does it return on equal values?

Coming from another language?

Java: a == null ? b : b == null ? a : a >= b ? a : b. Python: max(a, b) returns the first of equal values, as >? does. Kotlin: listOfNotNull(a, b).maxOrNull(). EK9: left >? right — one operator handles all cases including equality, and the left operand wins a tie.

Keywords: >?, coalescing, equal, float, higher, maximum, temperature, threshold, tie