Why does EK9 care about operator placement (prefix vs suffix)?

← Getting Started · Ref: Q794

EK9 has strict rules about operator position. Some operators must come BEFORE the expression (prefix), and one operator must come AFTER (suffix).

PREFIX OPERATORS (must come BEFORE)

  $value       converts to String
  $$value      converts to JSON
  #? collection   gets length or hashcode
  #< list      gets first element
  #> list      gets last element
  ~ number     negates or reverses
  not flag     boolean negation
  abs number   absolute value
  sqrt number  square root
  empty list   checks if empty
  length name  gets length

SUFFIX OPERATOR (must come AFTER)

  value?       checks if value is set (isSet)

COMMON MISTAKES

  list#<       WRONG: #< is prefix, use '#< list'
  value$       WRONG: $ is prefix, use '$value'
  ?value       WRONG: ? is suffix, use 'value?'

See Q25 for the promote operator. See Q39 for tri-state and isSet.

Example

defines module qa.gettingstarted.operatorplacement

  defines program

    OperatorPlacementDemo()
      -> userName as String
      stdout <- Stdout()

      nameText <- $userName
      stdout.println(nameText)

      nameLength <- length userName
      stdout.println(`Length: ${nameLength}`)

      if userName?
        stdout.println("User name is set")

Common mistakes

E01084 — The '$' operator is a prefix operator and must come BEFORE the expression. Write '$userName' not 'userName$'. See ek9 -h E01084 for details.

Incorrect:

      nameText <- userName$

Correct:

      nameText <- $userName

E01085 — The '?' operator is a suffix operator and must come AFTER the expression. Write 'userName?' not '?userName'. See ek9 -h E01085 for details.

Incorrect:

      if ?userName

Correct:

      if userName?
Other ways to ask this
  • What triggers E01084 prefix operator wrong position?
  • What triggers E01085 suffix operator wrong position?
  • Why does 'list$' fail but '$list' works?

Coming from another language?

Java: all operators are infix or prefix (++x, !flag). Python: not is prefix, no suffix operators. Rust: ! is prefix (boolean), ? is suffix (error propagation, similar to EK9). Kotlin: !! is suffix (non-null assertion), ! is prefix. Go: ! is prefix, no suffix operators. EK9: strict prefix/suffix distinction enforced at the grammar level.

Keywords: prefix, E01085, E01084, isSet, suffix, operator, string, position, length