Find the highest bid from two optional auction offers.

← Operators and Expressions · Ref: Q1214

The >? coalescing maximum operator returns the greater of two Money values, handling unset gracefully.

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

If both bids are set, >? returns the higher one. If one is unset (no bid placed), it returns the available bid. If both are unset, the result is unset.

This is a COMPARISON operator — it picks the larger 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 Q1209 for <? minimum on Money.

Example

defines module qa.operators.coalescemax.money

  defines function

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

  defines program

    CoalesceMaxMoneyDemo()
      stdout <- Stdout()

      // === BOTH BIDS RECEIVED — returns the higher ===

      bidderAlpha <- 5000.00#USD
      bidderBravo <- 7500.00#USD
      winningBid <- higherBid(bidderAlpha, bidderBravo)
      stdout.println(`Winning bid: ${winningBid}`)

      // === ONE BIDDER DIDN'T BID — returns the available bid ===

      activeBid <- 3200.00#USD
      missingBid <- Money()
      onlyBid <- higherBid(activeBid, missingBid)
      stdout.println(`Only bid: ${onlyBid}`)

      // === NO BIDS — result is unset ===

      missingA <- Money()
      missingB <- Money()
      bothMissing <- higherBid(missingA, missingB)
      if bothMissing?
        stdout.println(`Bid: ${bothMissing}`)
      else
        stdout.println("No bids received")

Common mistakes

E50060 — Money has no max() method; use the >? coalescing-maximum operator, which handles unset values and returns the greater. See ek9 -h E50060 for details.

Incorrect:

      <- rtn as Money: left.max(right)

Correct:

      <- rtn as Money: left >? right
Other ways to ask this
  • How do I select the maximum of two Money values when one bidder might not have bid?
  • An auction receives two bids but one could be missing — pick the higher one safely.
  • In Java I'd need null checks before comparing BigDecimal bids. What does EK9 use?
  • Migrating from Python where I use max() with None filtering for prices — what is the EK9 pattern?

Coming from another language?

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

Keywords: coalescing, auction, maximum, >?, money, higher, bid, offer