E08010: used before definition

Category: E08xxx Pre-IR checks (flow analysis) · Reported in phase: FULL_RESOLUTION · Classification: USED_BEFORE_DEFINED · All error codes

What went wrong

You're using an identifier (variable name) BEFORE it has been declared, or inside its own initialiser ('total as Integer: total + 1'). EK9 requires declaration before use. A PROPERTY read in a property initialiser before its own initialiser has run is not this error - that is E08060, however it is spelt.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 632 (How does EK9 ensure variables are used only after they ar...)
  • ek9 -q 688 (How does EK9 enforce variable initialization order?)

How to fix it

  1. Declare before use
  2. Check for typos in identifier name
  3. Ensure variable is in accessible scope

Common causes

  • Typo in variable name (using undeclared identifier)
  • Using variable before its declaration line
  • Scope confusion (variable declared in different block)
  • A local's initialiser reading the local itself

See also

  • E08010: identifier doesn't exist yet (this error)
  • E08020: exists but no value assigned
  • E08060: a property read before its initialiser has run

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E08010: used before definition

#!ek9
defines module error.e08010.bad

  defines function

    greeting()
      <- rtn as String?
      rtn: salutation + ", World"
      salutation <- "Hello"

Fixed: Declare the variable before the line that reads it

#!ek9
defines module error.e08010.good

  defines function

    greeting()
      <- rtn as String?
      salutation <- "Hello"
      rtn: salutation + ", World"

Questions and answers

These Q&A entries show E08010 and its fix: