What is the complete # operator family in EK9?

← Operators and Expressions · Ref: Q919

EK9 has exactly FOUR operators that use the # character. No others exist.

THE COMPLETE # OPERATOR LIST

#? — HASHCODE

Calls _hashcode(), returns Integer:

  hash <- #? myObject

Used for Dict keys and equality support.

#^ — PROMOTE

Calls _promote(), returns a WIDER type:

  asFloat <- #^ anInteger

Converts a value to a more general type.

#< — PREFIX

Calls _prefix(), returns the opening part:

  first <- #< text

On String, returns the first character.

#> — SUFFIX

Calls _suffix(), returns the closing part:

  last <- #> text

On String, returns the last character.

OPERATORS THAT DO NOT EXIST

These are NOT valid EK9 operators:
- # alone (not a comment, not an operator)
- ## (does not exist)
- #! (only valid as shebang on line 1)
- #$ (does not exist)
- #@ (does not exist)
- #: (does not exist)
- #{ (does not exist)

SUMMARY TABLE

  #?  _hashcode()  -> Integer     hash value for Dict keys
  #^  _promote()   -> wider type  type widening/promotion
  #<  _prefix()    -> varies      extract prefix/first part
  #>  _suffix()    -> varies      extract suffix/last part

All four must be declared 'as pure' because they extract information without side effects.

See Q911 for #? in detail. See Q912 for #^ in detail. See Q242 for all conversion operators.

Example

defines module qa.operators.hashsummary

  defines record

    Coord
      xPos as Float: 0.0
      yPos as Float: 0.0

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

      default operator

  defines program

    HashSummaryDemo()
      stdout <- Stdout()

      // === #? hashcode — returns Integer ===

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

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

      coord <- Coord(3.0, 4.0)
      coordHash <- #? coord
      stdout.println(`#? Record hash: ${coordHash}`)

      // === #^ promote — returns wider type ===

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

      // === #< prefix — returns first part ===

      word <- "Hello"
      first <- #< word
      stdout.println(`#< prefix: ${first}`)

      // === #> suffix — returns last part ===

      last <- #> word
      stdout.println(`#> suffix: ${last}`)

      // === All four on one value ===

      sample <- "World"
      stdout.println(`Value: ${sample}`)
      stdout.println(`  #? hash: ${(#? sample)}`)
      stdout.println(`  #< prefix: ${(#< sample)}`)
      stdout.println(`  #> suffix: ${(#> sample)}`)

Common mistakes

E50060 — EK9 does not have a hashCode() method like Java. Use the #? prefix operator to get the hashcode of any value. The #? calls the _hashcode() method. See ek9 -h E50060 for details.

Incorrect:

numHash <- number.hashCode()

Correct:

numHash <- #? number
Other ways to ask this
  • What # operators exist in EK9?
  • Are there operators like ##, #!, #$ in EK9?
  • What are all the hash operators in EK9?

Coming from another language?

Java: hashCode() method, no promote/prefix/suffix operators. Python: hash() function, # is comment. Rust: Hash trait, no prefix/suffix operators. Go: no hash interface, no operator symbols. Kotlin: hashCode() method, no operator symbols. EK9: #? hashcode, #^ promote, #< prefix, #> suffix — four operators, all pure.

Keywords: prefix, complete, hash, hashcode, family, promote, summary, suffix, operator