What does the #^ promote operator do in EK9?

← Operators and Expressions · Ref: Q969

The #^ operator is the PROMOTE operator in EK9. It calls the _promote() method on the object and returns a DIFFERENT type (type widening/conversion).

Syntax: #^ variableName (prefix operator)

IMPORTANT: #^ is NOT a Python comprehension. It is NOT a Ruby hash. The # prefix in EK9 means 'introspection' — #^ is promote, #? is hashcode, #< is first, #> is last.

EK9 introspection operators:

  #^ calls _promote() — type CONVERSION to a wider type
  #? calls _hashcode() — returns Integer hashcode
  #< calls _first() — returns first value (enumerations)
  #> calls _last() — returns last value (enumerations)

Typical use: converting a Character to a String, or a narrower numeric type to a wider one.

The 'default operator' keyword auto-generates #^ from declared fields. You rarely implement it manually.

Bridge: Like Java's widening conversion (int to long) but as an explicit operator. Like Rust's From/Into traits but with operator syntax.

Example

defines module qa.operators.promotedetail

  defines program

    PromoteDemo()
      stdout <- Stdout()

      // #^ on Character — promotes to String
      letter <- 'A'
      letterAsString <- #^ letter
      stdout.println(`Character promoted to String: ${letterAsString}`)

      // #^ on Integer — may promote to Float depending on type
      wholeNumber <- 42
      promoted <- #^ wholeNumber
      stdout.println(`Integer promoted: ${promoted}`)

      // The promote operator returns a DIFFERENT type
      // This is what distinguishes it from $ (string conversion)
      // $ always returns String, #^ returns whatever _promote() is defined to return
Other ways to ask this
  • How do I convert types in EK9?
  • What is #^ in EK9?
  • How does type promotion work in EK9?
  • What does hash caret mean in EK9?

Coming from another language?

Java: implicit widening (int -> long) or explicit casting. Python: int(x), str(x) conversion functions. EK9: #^ prefix operator calls _promote(). The # prefix is EK9's introspection family — NOT a comment.

Keywords: prefix operator, widening, type conversion, introspection, promote