E08060: is/may not be initialised before use

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

What went wrong

A property might not be initialized at the point where you're using it. This includes a property initialiser reading a property of its own type that has no value yet - one declared later, itself, or one the constructor sets. Initialisers run in declaration order, BEFORE the constructor body. Write the plain name ('later'): an initialiser may not use 'this' at all, so 'this.later' is E08061 (the object is not constructed yet).

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 104 (Why does EK9 require explicit constructors for uninitiali...)
  • ek9 -q 1397 (In what order do property initialisers run, and which pro...)

How to fix it

  1. Ensure initialization on all paths before use
  2. Or initialize at declaration
  3. Use guard syntax to handle unset case
  4. In a property initialiser, read only EARLIER properties that have their own initialiser - reorder the declarations, or compute the value in the constructor or a method

Common causes

  • Variable only initialized in conditional block
  • Using variable before initialization completes
  • A property initialiser reading a later property, itself, or a property the constructor sets

See also

  • E08060: property might not be initialized (this error)
  • E08050: return not initialized on all paths
  • E08010: a LOCAL used before it is declared, or in its own initialiser
  • E08061: the object itself (its methods, 'this') used before construction

Examples

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

Fails with E08060: is/may not be initialised before use

#!ek9
defines module error.e08060.bad

  defines class

    Stock
      available as Integer: total - reserved
      reserved as Integer: 2
      total as Integer: 12

      default operator ?

Fixed: Declare and initialise each property before one that reads it

#!ek9
defines module error.e08060.good

  defines class

    Stock
      reserved as Integer: 2
      total as Integer: 12
      available as Integer: total - reserved

      default operator ?

Questions and answers

These Q&A entries show E08060 and its fix: