Set a default tax rate only when the rate hasn't been configured.

← Control Flow · Ref: Q1224

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.

  taxRate <- Float()
  defaultRate <- 0.20
  taxRate :=? defaultRate
  //taxRate is now 0.20 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.float

  defines function

    lookupRegionalRate()
      <- rtn <- Float()
      //Simulates: no regional tax rate configured

    lookupNationalRate()
      <- rtn <- 0.15

  defines program

    GuardAssignFloatDemo()
      stdout <- Stdout()

      // === RATE IS UNSET — :=? assigns the default ===

      taxRate <- Float()
      defaultRate <- 0.20
      taxRate :=? defaultRate
      stdout.println(`Tax rate: ${taxRate}`)

      // === RATE IS ALREADY CONFIGURED — :=? is skipped ===

      customRate <- lookupNationalRate()
      customRate :=? defaultRate
      stdout.println(`Custom rate preserved: ${customRate}`)

      // === FALLBACK CHAIN — first set value wins ===

      effectiveRate <- Float()
      effectiveRate :=? lookupRegionalRate()
      effectiveRate :=? lookupNationalRate()
      effectiveRate :=? defaultRate
      stdout.println(`Effective rate: ${effectiveRate}`)

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 taxRate == null
        taxRate := defaultRate

Correct:

      taxRate :=? defaultRate
Other ways to ask this
  • How do I conditionally assign a Float only when the variable is unset?
  • A tax calculator has an optional rate — apply the standard rate if not configured.
  • In Java I'd check if taxRate == null before assigning a double default. What does EK9 use?
  • Migrating from Go where I check if rate == 0.0 — what is the EK9 pattern for default floats?

Coming from another language?

Java: if (taxRate == null) taxRate = 0.20. Python: tax_rate = tax_rate or 0.20 (fails for rate 0.0). Go: if rate == 0.0 { rate = 0.20 } (but 0.0 might be valid). EK9: taxRate :=? defaultRate — correct tri-state semantics, 0.0 is a valid set value.

Keywords: configuration, guarded, default, rate, float, assignment, :=?, tax