Does # mean comment in EK9 like Python?

← Operators and Expressions · Ref: Q914

No. In EK9, # is NOT a comment character. EK9 uses // for single-line comments. The # character appears ONLY in two specific operator symbols: #? and #^. That is it.

#? = HASHCODE

The #? operator calls _hashcode() and returns an Integer:

  hash <- #? value

This is analogous to Java's hashCode() but uses operator syntax.

#^ = PROMOTE

The #^ operator calls _promote() for type widening:

  wider <- #^ narrowValue

This converts to a wider type (e.g., Integer to Float).

THAT IS THE COMPLETE LIST

There are exactly TWO operators using #:
- #? (hashcode) — returns Integer
- #^ (promote) — returns a wider type

There are also #< (prefix) and #> (suffix) operators:
- #< extracts a prefix (e.g., first character of String)
- #> extracts a suffix (e.g., last character of String)

NO OTHER # OPERATORS EXIST

These do NOT exist in EK9:
- # alone (not a comment, not an operator)
- ## (not an operator)
- #! (only valid on first line as shebang)
- #$ (not an operator)
- #@ (not an operator)

COMMENTS IN EK9

// This is a comment in EK9
/* This is also a comment in EK9 */

See Q911 for #? in detail. See Q912 for #^ in detail. See Q903 for all operators that do not exist.

Example

defines module qa.operators.hashnotpython

  defines record

    Point
      xPos as Float: 0.0
      yPos as Float: 0.0

      Point()
        ->
          x as Float
          y as Float
        this.xPos :=: x
        this.yPos :=: y

      default operator

  defines program

    HashOperatorsDemo()
      stdout <- Stdout()

      // === #? on various types ===

      name <- "Hello"
      nameHash <- #? name
      stdout.println(`String hash: ${nameHash}`)

      number <- 42
      numberHash <- #? number
      stdout.println(`Integer hash: ${numberHash}`)

      point <- Point(3.0, 4.0)
      pointHash <- #? point
      stdout.println(`Point hash: ${pointHash}`)

      // === #^ promote ===

      intVal <- 100
      floatVal <- #^ intVal
      stdout.println(`Promoted: ${floatVal}`)

      // === #< prefix and #> suffix ===

      text <- "Hello"
      first <- #< text
      last <- #> text
      stdout.println(`Prefix: ${first}`)
      stdout.println(`Suffix: ${last}`)

      // === Comments use // not # ===
      // This is a valid EK9 comment

      stdout.println("# is NOT a comment in EK9")

Common mistakes

E07620 — The #^ (promote) operator is not defined on the Point record. Point has #? (hashcode) via 'default operator' but not #^. Use #? for hashcode. See ek9 -h E07620 for details.

Incorrect:

pointHash <- #^ point

Correct:

pointHash <- #? point
Other ways to ask this
  • Is # a comment character in EK9?
  • What do #? and #^ mean in EK9?
  • How is # different in EK9 versus Python?

Coming from another language?

Python: # is line comment. Ruby: # is line comment, #{ } is string interpolation. Shell: # is line comment. EK9: # is NOT a comment, // is line comment, #? is hashcode operator, #^ is promote operator, #< is prefix, #> is suffix.

Keywords: prefix, hash, hashcode, not-comment, promote, comment, python, suffix, operator