Get the smaller of two prices, even if one might be unset.
← Operators and Expressions · Ref: Q1092
Coalescing min (<?) returns the smaller value, max (>?) returns the larger:
cheapest <- priceA <? priceB topScore <- scoreA >? scoreB
//With one unset — returns the set one knownPrice <- 49.99 unknownPrice <- Float() best <- knownPrice <? unknownPrice //best is 49.99 — the set value wins
<? and >? are unique to EK9. If both are set, returns the smaller/larger. If one is unset, returns the set one. If both unset, result is unset.
See Q899 for all coalescing operators. See Q1091 for ?: elvis.
Example
defines module qa.operators.minmaxcoalescing defines function cheaperOf() as pure -> left as Float right as Float <- rtn as Float: left <? right higherOf() as pure -> left as Integer right as Integer <- rtn as Integer: left >? right defines program MinMaxCoalescingDemo() stdout <- Stdout() //Both set — returns the smaller cheapest <- cheaperOf(29.99, 19.99) stdout.println(`Cheapest: ${cheapest}`) //One unset — returns the set one knownPrice <- 49.99 unknownPrice <- Float() best <- knownPrice <? unknownPrice stdout.println(`Best available: ${best}`) //Coalescing maximum — both set, returns the larger topScore <- higherOf(85, 92) stdout.println(`Top score: ${topScore}`)
Common mistakes
E50001 — EK9 has no Math type, so Math.min(...) cannot be resolved — use the <? coalescing minimum operator instead. See ek9 -h E50001 for details.
Incorrect:
Math.min(knownPrice, unknownPrice)
Correct:
knownPrice <? unknownPrice
Other ways to ask this
- I have two price values and one could be missing — find the minimum safely
- In Java I'd need null checks before Math.min(). Write the safe EK9 version
- Given two optional sensor readings, return the lower one using <? coalescing
- Select the minimum of two values with automatic unset handling
Coming from another language?
Java: a == null ? b : b == null ? a : Math.min(a, b). Python: min(x for x in [a, b] if x is not None). EK9: a <? b — one operator handles all cases.
Keywords: <?, safe, coalescing, unset, >?, minimum, maximum