Get the higher score from two optional exam results.

← Operators and Expressions · Ref: Q1217

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

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

If both scores are set, >? returns the higher one. If one is unset (exam not taken), it returns the available score. If both are unset, the result is unset.

This is a COMPARISON operator — it picks the larger value. Do not confuse it with :=? (guarded assignment), which assigns only if the target is unset. See Q1226 for a side-by-side contrast.

See Q1092 for min/max coalescing overview. See Q1212 for <? minimum on Integer.

Example

defines module qa.operators.coalescemax.integer

  defines function

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

  defines program

    CoalesceMaxIntegerDemo()
      stdout <- Stdout()

      // === BOTH EXAMS TAKEN — returns the higher score ===

      midtermScore <- 78
      practicalScore <- 85
      bestScore <- higherScore(midtermScore, practicalScore)
      stdout.println(`Best score: ${bestScore}`)

      // === ONE EXAM NOT TAKEN — returns the available score ===

      writtenScore <- 92
      absentScore <- Integer()
      onlyScore <- higherScore(writtenScore, absentScore)
      stdout.println(`Available score: ${onlyScore}`)

      // === BOTH EXAMS MISSED — result is unset ===

      missedA <- Integer()
      missedB <- Integer()
      noScore <- higherScore(missedA, missedB)
      if noScore?
        stdout.println(`Score: ${noScore}`)
      else
        stdout.println("No exam scores recorded")

Common mistakes

E50001 — EK9 has no 'Math.max' and the 'Math' reference does not resolve; use the '>?' coalescing maximum operator instead. See ek9 -h E50001 for details.

Incorrect:

      <- rtn as Integer: Math.max(left, right)

Correct:

      <- rtn as Integer: left >? right
Other ways to ask this
  • How do I find the maximum of two Integer values when one student might not have taken the exam?
  • A grading system has two exam scores but one might be absent — pick the higher score.
  • In Java I'd need Optional and Math.max for nullable integers. What does EK9 use?
  • Migrating from Python where I use max() with None filtering — what is the EK9 pattern?

Coming from another language?

Java: a == null ? b : b == null ? a : Math.max(a, b). Python: max(s for s in [a, b] if s is not None). Go: custom function with nil checks. EK9: left >? right — one operator handles all cases.

Keywords: coalescing, grading, higher, score, >?, maximum, exam, integer