Why does AI confuse EK9 operators with other languages?

← What AI Gets Wrong About EK9 · Ref: Q279

AI models map Java and Python method names to EK9 when operators should be used. EK9 has a fixed set of operators with specific symbols. AI frequently generates method calls that do not exist.

COMMON AI MISTAKES AND CORRECT EK9

  .toString() does not exist. Use $ (the string operator). Always returns String.
  .toJson() does not exist. Use $$ (the JSON operator). Always returns JSON.
  .hashCode() does not exist. Use #? (the hashcode operator). Returns Integer.
  .equals(other) does not exist. Use == (equality operator). Returns Boolean.
  .clone() does not exist. Use :=: (copy operator) or copy constructor MyType(original).
  .isPresent() does not exist. Use ? (isSet operator). Returns Boolean.
  !x for logical NOT is wrong for Boolean. Use ~x (tilde, only for Boolean and Bits).
  x instanceof T does not exist. Use traits and dispatching instead.

OPERATOR DEFINITIONS IN A CLASS

When defining operators on your own types, use the 'operator' keyword:

  operator $ as pure
    <- rtn as String: ...
  operator #? as pure
    <- rtn as Integer: ...
  operator == as pure
    -> other as MyType
    <- rtn as Boolean: ...
  operator <=> as pure
    -> other as MyType
    <- rtn as Integer: ...

NOT-EQUAL

Both != and <> are valid for not-equal in EK9. AI often uses only != which works, but <> is also correct and preferred in some contexts.

COPY PATTERNS

Two ways to copy: the :=: operator mutates the target, the copy constructor creates a new instance:

  target :=: source
  copied <- MyType(original)

See Q238 for the complete operator set. See Q239 for comparison operators. See Q240 for arithmetic operators. See Q241 for mutation operators. See Q281 for verifying AI code.

Example

defines module qa.ai.mistakes.operators

  defines class

    // === Class to test $ operator confusion ===

    Labelled
      label as String: String()

      Labelled()
        -> initialLabel as String
        label: initialLabel

      operator $ as pure
        <- rtn as String: String(label)

      override operator ? as pure
        <- rtn as Boolean: label?

    // === Class to test #? operator confusion ===

    Coded
      code as Integer: Integer()

      Coded()
        -> initialCode as Integer
        code: initialCode

      operator #? as pure
        <- rtn as Integer: Integer(code)

      override operator ? as pure
        <- rtn as Boolean: code?

    // === Class to test == operator confusion ===

    Ranked
      rank as Integer: Integer()

      Ranked()
        -> initialRank as Integer
        rank: initialRank

      operator == as pure
        -> other as Ranked
        <- rtn as Boolean: rank == other.rank

      override operator ? as pure
        <- rtn as Boolean: rank?

  defines program

    OperatorConfusionDemo()
      stdout <- Stdout()

      // === $ REPLACES toString() ===

      item <- Labelled("hello")
      stdout.println(`Item: ${item}`)

      // === #? REPLACES hashCode() ===

      coded <- Coded(42)
      hash <- #?coded
      stdout.println(`Hash: ${hash}`)

      // === == REPLACES equals() ===

      rankA <- Ranked(1)
      rankB <- Ranked(1)
      stdout.println(`Equal: ${rankA == rankB}`)

      // === ~ FOR BOOLEAN NOT ===

      active <- true
      inactive <- ~active
      stdout.println(`Active: ${active}, Inactive: ${inactive}`)

Common mistakes

E04020 — EK9 has no toString() method. Use the $ operator for string conversion. Without it, the type cannot be used in string interpolation. See ek9 -h E04020 for details.

Incorrect:

toString() as pure

Correct:

operator $ as pure

E07620 — EK9 has no hashCode() method. Use the #? operator for hash code computation. Without it, the #? prefix operator is not available on the type. See ek9 -h E07620 for details.

Incorrect:

hashCode() as pure

Correct:

operator #? as pure

E07620 — EK9 has no equals() method. Use the == operator for equality comparison. Without it, the == infix operator is not available on the type. See ek9 -h E07620 for details.

Incorrect:

equals() as pure

Correct:

operator == as pure

E50001 — Renaming the variable means later references to 'item' become unresolved, triggering E50001. Variable names must be consistent. See ek9 -h E50001 for details.

Incorrect:

itemXYZ <- Labelled("hello")

Correct:

item <- Labelled("hello")

E50001 — Renaming the variable means later references to 'coded' become unresolved, triggering E50001. Variable names must be consistent. See ek9 -h E50001 for details.

Incorrect:

codedXYZ <- Coded(42)

Correct:

coded <- Coded(42)
Other ways to ask this
  • Why does AI generate toString() instead of the $ operator?
  • What are the correct EK9 operator equivalents?
  • How do I fix AI-generated method calls that should be operators?

Coming from another language?

Java: toString(), hashCode(), equals(), clone(), instanceof, !boolean. Python: str(), hash(), __eq__, copy.copy(), isinstance(), not. Rust: Display trait, Hash trait, PartialEq, Clone, matches!, !bool. Go: Stringer interface, no operator overloading. Kotlin: toString(), hashCode(), equals(), !boolean. EK9: $ for string, $$ for JSON, #? for hashcode, == for equality, :=: for copy, ? for isSet, ~ for NOT (Boolean/Bits only), traits and dispatching replace instanceof.

Keywords: isset, common-error, hashcode, instanceof, hallucination, operator, equals, pitfall, confuse, ai, mistake, migrate, clone, tostring, wrong