Apply a minimum price only if the product price hasn't been set.
← Control Flow · Ref: Q1223
The :=? guarded assignment operator assigns a value ONLY if the target variable is currently unset. If the variable already has a value, the assignment is skipped.
price <- Money() 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.
See Q1038 for :=? with config fallback chains.
Example
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 === price <- Money() 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 <- Money() 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 E01072 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: guarded, default, money, product, minimum, assignment, :=?, price