E07290: only constructors and operators methods are supported on records

Category: E07xxx Full resolution · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: RECORDS_ONLY_SUPPORT_CONSTRUCTOR_AND_OPERATOR_METHODS · All error codes

What went wrong

You're defining a regular method on a RECORD, but records can only have constructors and operators - not methods.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 97 (What is the difference between classes and records in EK9?)
  • ek9 -q 98 (How do record operators work in EK9?)
  • ek9 -q 119 (What EK9 constructs are available and when should I use e...)

Rationale

RECORDS are for pure DATA. Classes are for BEHAVIOR.

How to fix it

  1. Use CLASS instead if you need methods
  2. Or use standalone functions with record

Common causes

  • Treating record like a class
  • Trying to add behavior to data type

See also

  • E07290: method on record (this error)
  • E07075: traits cannot have properties
  • Records can have constructors and operators

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07290: only constructors and operators methods are supported on records

#!ek9
defines module error.e07290.bad

  defines record

    Point
      x <- 0
      y <- 0

      //A record carries data; behaviour belongs in a function or a class
      moveBy()
        -> distance as Integer
        <- rtn as Point: Point(x + distance, y + distance)

      default operator ?

Fixed: Use an operator, or a function over the record

#!ek9
defines module error.e07290.good

  defines record

    Point
      x as Integer?
      y as Integer?

      default private Point() as pure

      Point() as pure
        ->
          across as Integer
          down as Integer

        this.x :=? across
        this.y :=? down

      //Operators are allowed on a record
      default operator ?

      operator + as pure
        -> other as Point
        <- rtn as Point: Point(x + other.x, y + other.y)

  defines function

    //The behaviour is a function over the record
    moveBy() as pure
      ->
        point as Point
        distance as Integer
      <- rtn as Point: point + Point(distance, distance)

Questions and answers

These Q&A entries show E07290 and its fix: