What method does each EK9 operator call internally?

← Operators and Expressions · Ref: Q1003

Every EK9 operator calls a specific method on the object. Here are the key mappings:

CONVERSION OPERATORS:

  $variable      calls _string()    returns String
  #? variable    calls _hashcode()  returns Integer
  #^ variable    calls _promote()   returns a different type
  $$ variable    calls _json()      returns JSON String

CHECKING OPERATORS:

  variable?      calls _isSet()     returns Boolean (suffix, no parentheses)

COMPARISON OPERATORS:

  a == b         calls _eq()        returns Boolean
  a <> b         calls _neq()       returns Boolean
  a <=> b        calls _cmp()       returns Integer (-1, 0, 1)

COPY/MERGE OPERATORS:

  a :=: b        calls _copy()      copies b into a
  a :~: b        calls _merge()     merges b into a
  a :^: b        calls _replace()   replaces a content with b

The 'default operator' keyword auto-generates all of these from your declared fields. You only need to implement them manually for custom behaviour.

Example

defines module qa.operators.methodnames

  defines record

    Sensor
      location as String: String()
      reading as Float: 0.0

      Sensor()
        ->
          location as String
          reading as Float
        this.location :=: location
        this.reading :=: reading

      default operator

  defines program

    OperatorMethodNamesDemo()
      stdout <- Stdout()

      sensor <- Sensor("Kitchen", 22.5)

      // $ calls _string()
      sensorText <- $sensor
      stdout.println(`String: ${sensorText}`)

      // #? calls _hashcode()
      sensorHash <- #? sensor
      stdout.println(`Hash: ${sensorHash}`)

      // ? calls _isSet() — suffix, no parentheses
      stdout.println(`Is set: ${sensor?}`)

      // <=> calls _cmp()
      other <- Sensor("Lounge", 20.0)
      comparison <- sensor <=> other
      stdout.println(`Compare: ${comparison}`)

Common mistakes

E50060 — EK9 objects have no toString() method — the $ prefix operator calls _string(); use $variable (or ${...} interpolation) for String conversion. See ek9 -h E50060 for details.

Incorrect:

      stdout.println(`Is set: ${sensor.toString()}`)

Correct:

      stdout.println(`Is set: ${sensor?}`)
Other ways to ask this
  • What is the mapping between EK9 operators and their method names?
  • What does each EK9 operator symbol translate to?
  • Show me the operator to method name mapping in EK9

Coming from another language?

EK9 operators are method calls with fixed names. $ is _string() (like Java toString), #? is _hashcode() (like Java hashCode), ? is _isSet() (like Rust is_some).

Keywords: string, mapping, method, hashcode, promote, operator, name