Show both comparison coalescing operators on Integer values.
← Operators and Expressions · Ref: Q1227
EK9 has two comparison coalescing operators. Both 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
Both are COMPARISON operators - they compare two values and return one of them. They are NOT assignment operators. Do not confuse them with :=? (assign-if-unset), which is a completely different operation.
On a tie (the two values compare equal, <=> gives 0) both return the LEFT operand; swap the operands to get the right one. If one operand is unset the set one is returned; if both are unset the result is unset. The inclusive forms that once existed were removed (E01095): they are what <? and >? now do.
See Q1226 for the distinction between <? and :=?. See Q1092 for min/max coalescing basics.
Example
#!ek9 defines module qa.operators.allcoalescing defines function // <? — returns the LESSER of two values (the left one on a tie) minOf() as pure -> left as Integer right as Integer <- rtn as Integer: left <? right // >? — returns the GREATER of two values (the left one on a tie) maxOf() as pure -> left as Integer right as Integer <- rtn as Integer: left >? right defines program AllCoalescingOperatorsDemo() stdout <- Stdout() // === BOTH 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}`) // === 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 — both 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}`) // === WITH BOTH UNSET — both 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 comparison coalescing operators in EK9 and how do they differ?
- I need to see <? and >? side by side on the same type.
- In Java there's no equivalent to coalescing operators — show the EK9 pair.
- Demonstrate the comparison coalescing operators with Integer examples, including equal and unset values.
Coming from another language?
Java: requires 2 separate utility methods with null checks. Python: min/max handle None only with filtering, and return the first of equal values as <? and >? do. Kotlin: requires custom extension functions. EK9: <? and >? — two built-in operators that handle unset values automatically.
Keywords: <?, >?, all, coalescing, comparison, family, integer, tie