Check the set and unset states of a component's fields.

← Dependency Injection · Ref: Q1107

Check component set status with ?:

  settings <- Settings()
  if settings?
    stdout.println("Settings ready")

Field initialisers (e.g., name <- "default") make the component SET immediately after construction.

See Q1087 for default operator ? semantics. See Q1105 for defining components.

Example

defines module qa.di.componentissetstates

  defines component

    Settings
      theme <- "dark"
      fontSize <- 14

      getTheme()
        <- rtn as String: theme

      default operator

  defines program

    ComponentIsSetStatesDemo()
      stdout <- Stdout()

      //Component with field initialisers — set immediately
      settings <- Settings()
      stdout.println(`Settings set?: ${settings?}`)
      stdout.println(`Settings: ${settings}`)

Common mistakes

E01073 — EK9 has no null; use the ? operator to test whether a component has meaningful data instead of comparing to null. See ek9 -h E01073 for details.

Incorrect:

settings != null

Correct:

settings?
Other ways to ask this
  • I need to verify which fields in a component have been initialised
  • In Java I'd check for null fields on a bean. Show the EK9 component isSet pattern
  • Given a component with default values, confirm operator ? reflects field state
  • Test whether a component is set when its fields have initial values

Coming from another language?

Java: manual null checks on bean fields. Python: hasattr checks. EK9: operator ? on the component — any-field semantics by default.

Keywords: state, isSet, ?, component, fields, initialised