Check which fields are set in a partially-initialised record.

← Operators and Expressions · Ref: Q1088

A partially-initialised object is still 'set' under default ?:

  partial <- ContactInfo("Alice")
  if partial?
    stdout.println("set")

With 1 of 3 fields set, default ? returns true (ANY-field semantics).

See Q1087 for basic isSet. See Q1089 for custom override operator ?.

Example

defines module qa.operators.checkpartialset

  defines class

    ContactInfo
      name <- String()
      email <- String()
      phone <- String()

      ContactInfo()
        -> name as String
        this.name :=: name
        //email and phone remain unset

      default operator

  defines program

    CheckPartialSetDemo()
      stdout <- Stdout()

      //Only name is set — email and phone remain unset
      partial <- ContactInfo("Alice")
      stdout.println(`Contact: ${partial}`)

      //default ? uses ANY-field semantics: 1 of 3 is set, so true
      stdout.println(`Is set?: ${partial?}`)

      //Compare with fully unset object
      blank <- ContactInfo()
      stdout.println(`Empty set?: ${blank?}`)

Common mistakes

E01073 — 'null' does not exist in EK9 — check an object's overall set-status with the '?' operator (partial?) instead of comparing to null. See ek9 -h E01073 for details.

Incorrect:

${partial <> null}

Correct:

${partial?}
Other ways to ask this
  • I have a contact with only a name filled in — verify that ? still returns true
  • In Java I'd check each field for null separately. Show the EK9 ANY-field semantics
  • Given an object with 1 of 3 fields set, confirm the default operator ? result
  • Test whether a partially-initialised object counts as set in EK9

Coming from another language?

Java: check each field != null individually. Python: hasattr or getattr checks. EK9: default ? checks any field; override ? for custom logic.

Keywords: individual, check, partial, unset, field, isSet, ?