What is the difference between < and <? in EK9?
← Operators and Expressions · Ref: Q1067
They do completely different things:
< COMPARES and returns Boolean:
isSmaller <- 5 < 10 returns true isSmaller is a Boolean.
<? COMPARES and returns the LESSER VALUE:
smaller <- 5 <? 10 returns 5 smaller is an Integer (same type as the operands).
Side by side:
5 < 10 returns true (Boolean: is 5 less than 10?) 5 <? 10 returns 5 (Integer: the lesser of 5 and 10) 10 < 5 returns false (Boolean: is 10 less than 5?) 10 <? 5 returns 5 (Integer: still the lesser value)
The same pattern applies to the other coalescing operators:
> returns Boolean >? returns the GREATER value <= returns Boolean <=? returns left if left <= right >= returns Boolean >=? returns left if left >= right
<? is NOT less-than. It is coalescing minimum. It returns a value, not a Boolean.
See Q963 for <? in detail. See Q964 for >? in detail. See Q1050 for coalescing on classes.
Example
defines module qa.operators.lessthanvscoalescing defines function lesserOf() as pure -> left as Integer right as Integer <- rtn as Integer: left <? right greaterOf() as pure -> left as Integer right as Integer <- rtn as Integer: left >? right defines program LessThanVsCoalescingDemo() stdout <- Stdout() scoreA <- 85 scoreB <- 92 //< returns Boolean isLess <- scoreA < scoreB stdout.println(`${scoreA} < ${scoreB} = ${isLess}`) //<? returns the lesser VALUE lesser <- lesserOf(scoreA, scoreB) stdout.println(`${scoreA} <? ${scoreB} = ${lesser}`) //>? returns the greater VALUE greater <- greaterOf(scoreA, scoreB) stdout.println(`${scoreA} >? ${scoreB} = ${greater}`) //They are different types of result stdout.println(`< gives Boolean: ${isLess}`) stdout.println(`<? gives Integer: ${lesser}`)
Common mistakes
E50001 — EK9 has no Math class; use the <? coalescing operator to get the lesser of two values. See ek9 -h E50001 for details.
Incorrect:
<- rtn as Integer: Math.min(left, right)
Correct:
<- rtn as Integer: left <? right
Other ways to ask this
- How do < and <? differ in EK9?
- Is <? the same as less-than in EK9?
- Contrast the < comparison with the <? coalescing operator
Coming from another language?
Java: Math.min(a,b) for minimum value, a < b for comparison. Python: min(a,b) vs a < b. EK9: a <? b for minimum value, a < b for comparison.
Keywords: than, comparison, coalescing, less, boolean, minimum, value