What do <? and >? mean in EK9 — are they null coalescing?

← Operators and Expressions · Ref: Q939

No. <? and >? are MINIMUM and MAXIMUM coalescing operators, not null coalescing.

<? RETURNS THE SMALLER VALUE

  minimum <- 10 <? 3       // Result: 3
  minimum <- 7 <? 20       // Result: 7

>? RETURNS THE LARGER VALUE

  maximum <- 10 >? 3       // Result: 10
  maximum <- 7 >? 20       // Result: 20

UNSET HANDLING

When one operand is unset, the other is returned:

  a <- 5
  b <- Integer()
  result <- a <? b         // Result: 5 (b is unset, a wins)

NOT NULL COALESCING

For null/unset coalescing, EK9 has separate operators:

  ?? checks memory presence (absent check)
  ?: checks isSet state (elvis operator)
  :=? assigns only if target is unset

REMEMBER

  <? = minimum (pick smaller)
  >? = maximum (pick larger)
  ?? = present check (memory)
  ?: = set check (isSet)

See Q243 for all coalescing operators. See Q239 for comparison operators.

Example

defines module qa.operators.coalescingminmax

  defines function

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

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

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

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

    chainedMin() as pure
      ->
        a as Integer
        b as Integer
        c as Integer
      <- rtn as Integer: a <? b <? c

    chainedMax() as pure
      ->
        a as Integer
        b as Integer
        c as Integer
      <- rtn as Integer: a >? b >? c

  defines program

    CoalescingMinMaxDemo()
      stdout <- Stdout()

      //<? gives the smaller value
      minResult <- minOf(15, 8)
      stdout.println(`min(15, 8) = ${minResult}`)

      //>? gives the larger value
      maxResult <- maxOf(15, 8)
      stdout.println(`max(15, 8) = ${maxResult}`)

      //Works with Float too
      smallFloat <- minOfFloat(3.14, 2.72)
      stdout.println(`min(3.14, 2.72) = ${smallFloat}`)

      bigFloat <- maxOfFloat(3.14, 2.72)
      stdout.println(`max(3.14, 2.72) = ${bigFloat}`)

      //Chained: find minimum of three values
      smallest <- chainedMin(20, 5, 12)
      stdout.println(`min(20, 5, 12) = ${smallest}`)

      largest <- chainedMax(20, 5, 12)
      stdout.println(`max(20, 5, 12) = ${largest}`)
Other ways to ask this
  • Is <? the null coalescing operator in EK9?
  • How do min/max coalescing operators work in EK9?
  • What is the difference between <? and ?? in EK9?

Coming from another language?

Perl: min/max functions. Ruby: [a,b].min and [a,b].max. Python: min(a,b) and max(a,b). Rust: std::cmp::min/max. Java: Math.min/max. EK9: <? and >? as infix operators with unset-safe coalescing.

Keywords: coalescing, compare, lesser, operator, unset, greater, minimum, maximum