The compiler reports E08030 on this method. What is wrong and how do I fix it?

← Debugging and Troubleshooting · Ref: Q993

E08030 means you are accessing a value that might be unset without checking first. The compiler tracks which variables are guaranteed to be set at each point in the code.

IN THIS CODE:

The lookupDiscount function returns a Float that might be unset (returns Float() for unknown products). The calling code uses the result directly without checking if it is set.

THE FIX:

Use a guard expression to check isSet before accessing the value:

  if discountRate <- lookupDiscount(productCode)
    finalPrice := basePrice - (basePrice * discountRate)

The guard 'if discountRate <- lookupDiscount(productCode)' declares discountRate AND checks if it is set. The body only runs when the discount is available.

ALTERNATIVELY:

Use the ?? coalescing operator for a default:

  discountRate <- lookupDiscount(productCode) ?? 0.0
  finalPrice := basePrice - (basePrice * discountRate)

This assigns 0.0 (no discount) when the lookup returns unset.

Example

defines module qa.debugging.diagnoseerror

  defines function

    lookupDiscount() as pure
      -> productCode as String
      <- rtn as Float: Float()

      premiumCode <- "PREMIUM"
      standardCode <- "STANDARD"

      if productCode == premiumCode
        rtn: 0.20
      else if productCode == standardCode
        rtn: 0.10

  defines program

    DiagnoseErrorDemo()
      stdout <- Stdout()

      basePrice <- 100.0

      //Correct: use guard to check if discount exists
      if discountRate <- lookupDiscount("PREMIUM")
        finalPrice <- basePrice - (basePrice * discountRate)
        stdout.println(`Discounted price: ${finalPrice}`)

      //Correct: use ?? for default value
      standardDiscount <- lookupDiscount("UNKNOWN") ?? 0.0
      regularPrice <- basePrice - (basePrice * standardDiscount)
      stdout.println(`Regular price: ${regularPrice}`)

Common mistakes

E01073 — 'null' does not exist in EK9. Use a guard 'if v <- expr()' to check isSet, or the ? suffix: 'if discountRate?'. See ek9 -h E01073.

Incorrect:

      discountRate <- lookupDiscount("PREMIUM")
      if discountRate != null
        finalPrice <- basePrice - (basePrice * discountRate)
        stdout.println(`Discounted price: ${$finalPrice}`)

Correct:

      if discountRate <- lookupDiscount("PREMIUM")
        finalPrice <- basePrice - (basePrice * discountRate)
        stdout.println(`Discounted price: ${finalPrice}`)
Other ways to ask this
  • Diagnose this E08030 error in my EK9 code
  • Why is the compiler saying unsafe access on this line?
  • Help me understand and fix E08030 in this code

Coming from another language?

E08030 in EK9 is similar to 'potential null dereference' warnings in other languages, but in EK9 it is a hard compile error, not a warning.

Keywords: access, unsafe, diagnose, guard, fix, E08030