E08180: not marked for injection nor initialised

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

What went wrong

A variable or property is declared with a type alone ('name as String'): no initial value, and no '?' to say it may start unset. Give it a value, declare it 'name as String?' and assign it later, or (a component) mark it for injection with '!'.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 22 (How do I declare a variable in EK9?)
  • ek9 -q 1072 (Why does my abstract function get E08180 on the return...)
  • ek9 -q 5 (How are EK9 source files structured?)
  • ek9 -q 6 (How do I organize code into modules?)
  • ek9 -q 16 (How does indentation work in EK9?)
  • ek9 -q 25 (What is the promote operator?)

How to fix it

  1. Initialize it with a value ('name <- "Steve"', 'title as String: "untitled"')
  2. Or declare it unset with '?' and assign it later ('name as String?')
  3. Or, for a component, mark it for injection with '!' suffix

Common causes

  • Forgot to initialize property
  • Forgot to add '!' for injection
  • Property intended to be set later but not marked

See also

  • E08180: property not initialized or injected (this error)
  • E08170: injected variable reassignment

Examples

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

Fails with E08180: not marked for injection nor initialised

#!ek9
defines module error.e08180.bad

  defines record

    Book
      title as String

      default operator ?

  defines function

    greet()
      <- rtn as String?
      name as String
      rtn: "Hello " + name

Fixed: Give each declaration a value where it is declared

#!ek9
defines module error.e08180.good.fix

  defines record

    Book
      title as String: "untitled"

      default operator ?

  defines function

    greet()
      <- rtn as String?
      name <- "Steve"
      rtn: "Hello " + name

Fixed: Initialise it, or say it may be unset with ?

#!ek9
defines module error.e08180.good

  defines record

    Book
      title as String: "untitled"

      default operator ?

  defines function

    greet()
      <- rtn as String?
      name <- "Steve"
      rtn: "Hello " + name

    greetLater()
      -> known as Boolean
      <- rtn as String: "Hello"
      name as String?
      if known
        name: "Steve"
      else
        name: "friend"
      rtn: `${rtn} ${name}`

Questions and answers

These Q&A entries show E08180 and its fix: