Select the cheaper of two price quotes where one vendor hasn't responded.

← Operators and Expressions · Ref: Q1209

The <? coalescing minimum operator returns the lesser of two Money values, handling unset gracefully.

  cheaperOf() as pure
    -> left as Money, right as Money
    <- rtn as Money: left <? right

If both prices are set, <? returns the cheaper one. If one is unset (vendor hasn't responded), it returns the available quote. If both are unset, the result is unset.

This is a COMPARISON operator — it picks the smaller 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 Q1200 for Money arithmetic.

Example

defines module qa.operators.coalescemin.money

  defines function

    cheaperOf() as pure
      ->
        left as Money
        right as Money
      <- rtn as Money: left <? right

  defines program

    CoalesceMinMoneyDemo()
      stdout <- Stdout()

      // === BOTH QUOTES RECEIVED — returns the cheaper ===

      vendorA <- 149.99#USD
      vendorB <- 129.50#USD
      bestPrice <- cheaperOf(vendorA, vendorB)
      stdout.println(`Best price: ${bestPrice}`)

      // === ONE VENDOR HASN'T RESPONDED — returns the available quote ===

      receivedQuote <- 199.00#USD
      pendingQuote <- Money()
      onlyOption <- cheaperOf(receivedQuote, pendingQuote)
      stdout.println(`Available quote: ${onlyOption}`)

      // === NEITHER VENDOR RESPONDED — result is unset ===

      missingA <- Money()
      missingB <- Money()
      noQuote <- cheaperOf(missingA, missingB)
      if noQuote?
        stdout.println(`Quote: ${noQuote}`)
      else
        stdout.println("No vendor quotes available")

Common mistakes

E50060 — Money has no min() method; use the <? coalescing-minimum operator, which handles unset values and returns the lesser. See ek9 -h E50060 for details.

Incorrect:

left.min(right)

Correct:

left <? right
Other ways to ask this
  • How do I find the minimum of two Money values when one might be unset?
  • A procurement system has two vendor quotes but one is pending — pick the cheaper safely.
  • In Java I'd need null checks before comparing BigDecimal prices. What does EK9 use?
  • Migrating from Go where I check nil before comparing prices — what is the EK9 pattern?

Coming from another language?

Java: a == null ? b : b == null ? a : a.compareTo(b) <= 0 ? a : b. Python: min(p for p in [a, b] if p is not None). Rust: no built-in money type. EK9: left <? right — one operator handles all cases.

Keywords: coalescing, <?, money, minimum, quote, cheaper, price, vendor