E08060: is/may not be initialised before use
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
- Ensure initialization on all paths before use
- Or initialize at declaration
- Use guard syntax to handle unset case
- 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
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:
- Q104: Why does EK9 require explicit constructors for uninitialised properties?
- Q1346: Why does E07175 fire on my generic class with a 'T?' field and a default constructor?
- Q1347: When must a generic class use 'default private' for its no-argument constructor?
- Q1397: In what order do property initialisers run, and which properties can an initialiser read?