Why does EK9 reject name$ instead of $name?

← Syntax and Structure Rules · Ref: Q723

EK9 has several PREFIX operators that must appear BEFORE the expression they operate on. Placing them after the expression triggers E01084.

PREFIX OPERATORS

These operators extract values or transform expressions:
- $ (string) - converts to string: $value
- $$ (JSON) - converts to JSON: $$value
- #? (length/hashcode) - gets length or hashcode: #? collection
- ~ (negate/reverse) - negates or reverses: ~condition
- not (boolean negate) - negates boolean: not condition
- abs (absolute) - absolute value: abs number
- sqrt (square root) - square root: sqrt number
- empty (empty check) - checks if empty: empty collection
- length (length) - gets length: length collection

COMMON MISTAKE

Developers from languages like Ruby or method-chaining styles write 'value$' or 'name#?'. In EK9 these are prefix operators so the correct form is '$value' and '#? name'.

See Q724 for suffix operator positioning. See Q238 for the complete operator set.

Example

defines module qa.syntaxrules.prefixoperator

  defines function

    formatValues() as pure
      ->
        name as String
        score as Integer
      <- result as String: String()

      //Correct prefix operator usage
      nameStr <- $name
      scoreStr <- $score
      nameLen <- length name
      hash <- #? name
      absScore <- abs score

      result: `${nameStr} (${scoreStr}) len=${nameLen} hash=${hash} abs=${absScore}`

  defines program

    PrefixOperatorDemo()
      stdout <- Stdout()

      name <- "Steve"

      //Correct: prefix operators before expression
      stdout.println(`String: ${name}`)
      stdout.println(`Length: ${length name}`)
      stdout.println(`Hash: ${#? name}`)
      stdout.println(`Abs: ${abs -42}`)
      stdout.println(formatValues("Alice", 95))

Common mistakes

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

Incorrect:

stdout.println(formatValues("Alice", 95).toUpperCase())

Correct:

stdout.println(formatValues("Alice", 95))
Other ways to ask this
  • Why must prefix operators come before the expression?
  • What is E01084 prefix operator wrong position?
  • How do I use the $ operator correctly in EK9?

Coming from another language?

Java: value.toString() is method call syntax. Ruby: array.first is postfix. Python: str(value) is function call. JavaScript: String(value) is constructor. EK9: $value is prefix operator, not method call or function.

Keywords: syntax, E01084, length, position, operator, string, prefix, hashcode, wrong, negate