Why does EK9 reject ?name instead of name?

← Syntax and Structure Rules · Ref: Q724

EK9's ? (isSet) operator is a SUFFIX operator that must appear AFTER the expression it checks. Placing it before the expression triggers E01085.

SUFFIX OPERATOR

The main suffix operator in EK9:
- ? (isSet) - checks if value is set/valid: value?

COMMON MISTAKE

Developers from languages with prefix null checks (PHP isset(), Python is not None placed before) write '?value'. In EK9 the correct form is 'value?'.

USAGE PATTERNS

The ? operator is used extensively in EK9 for checking variable state:

  if name?
    stdout.println(name)

This checks whether name has been set to a meaningful value.

GUARD EXPRESSIONS

The ? operator combines with guard assignments:

  if record <- findRecord(key)
    process(record)

The guard implicitly checks isSet on the assignment result.

See Q723 for prefix operator positioning. See Q22 for variable declarations and the tri-state model.

Example

defines module qa.syntaxrules.suffixoperator

  defines function

    describeValue() as pure
      -> input as String
      <- description as String: "unset"

      //Correct: suffix ? after the variable
      if input?
        description: "set to " + input

  defines program

    SuffixOperatorDemo()
      stdout <- Stdout()

      name <- "Steve"
      unsetStr <- String()

      //Correct: ? operator after the expression
      stdout.println(`Name set: ${name?}`)
      stdout.println(`Unset set: ${unsetStr?}`)

      stdout.println(describeValue(name))
      stdout.println(describeValue(unsetStr))

      //Guard expression (implicit isSet check)
      first <- Optional("hello")
      if first?
        if present <- first.get()
          stdout.println(`Got: ${present}`)

Common mistakes

E50060 — String has no toUpperCase() method in EK9. Use upperCase() instead. See ek9 -h E50060 for details.

Incorrect:

stdout.println(describeValue(name).toUpperCase())

Correct:

stdout.println(describeValue(name))

E50060 — String has no toUpperCase() method in EK9. Use upperCase() instead. See ek9 -h E50060 for details.

Incorrect:

stdout.println(describeValue(unsetStr).toUpperCase())

Correct:

stdout.println(describeValue(unsetStr))
Other ways to ask this
  • Why must the isSet operator come after the variable?
  • What is E01085 suffix operator wrong position?
  • How do I check if a value is set in EK9?

Coming from another language?

Java: value != null is comparison. PHP: isset($value) is prefix function. Python: value is not None is infix. Ruby: value.nil? is suffix method. Kotlin: value != null or value?.method for null safety. EK9: value? is suffix operator, checks tri-state isSet.

Keywords: operator, wrong, set, position, check, E01085, syntax, tri-state, unset, isset, suffix