Apply a minimum price only if the product price hasn't been set.
← Control Flow · Ref: Q1223
The :=? assign-if-unset operator assigns a value ONLY if the target variable is currently unset. If the variable already has a value, the assignment is skipped.
price <- lookupCatalogPrice() //no catalog price: unset minimumPrice <- 9.99#USD price :=? minimumPrice //price is now 9.99 USD because it was unset
This is an ASSIGNMENT operator — it sets a variable. Use :=? where the value MAY be unset - here it comes from a lookup that can find nothing. Straight after 'price <- Money()' the compiler can see it is unset, so ':=?' would always apply: write ':=', or declare it with the value (E08097).
See Q1038 for :=? with config fallback chains.
Example
#!ek9 defines module qa.flow.guardassign.money defines function lookupCatalogPrice() <- rtn <- Money() //Simulates: no catalog price found lookupSuggestedPrice() <- rtn <- 24.99#USD defines program GuardAssignMoneyDemo() stdout <- Stdout() // === PRICE IS UNSET — :=? assigns the minimum === //No catalog price: unset price <- lookupCatalogPrice() minimumPrice <- 9.99#USD price :=? minimumPrice stdout.println(`Product price: ${price}`) // === PRICE IS ALREADY SET — :=? is skipped === existingPrice <- 49.99#USD existingPrice :=? minimumPrice stdout.println(`Existing price preserved: ${existingPrice}`) // === FALLBACK CHAIN — first set value wins === listPrice <- lookupCatalogPrice() listPrice :=? lookupSuggestedPrice() listPrice :=? minimumPrice stdout.println(`List price: ${listPrice}`)
Common mistakes
E01073 — EK9 has no null. Use :=? to conditionally assign — it only sets the value when the variable is currently unset. See ek9 -h E01073 for details.
Incorrect:
if price == null price := minimumPrice
Correct:
price :=? minimumPrice
Other ways to ask this
- How do I conditionally assign a Money value only when the variable is unset?
- A product listing has an optional price — apply a default minimum if not set.
- In Java I'd check if price == null before assigning a BigDecimal default. What does EK9 use?
- Migrating from Python where I use 'price = price or Decimal(default)' — what is the EK9 pattern?
Coming from another language?
Java: if (price == null) price = new BigDecimal("9.99"). Python: price = price or Decimal('9.99') (fails for Decimal(0)). Kotlin: price = price ?: minimumPrice. Go: no built-in money type. EK9: price :=? minimumPrice — one operator, correct tri-state semantics.
Keywords: :=?, assignment, default, guarded, minimum, money, price, product