E07040: 'by' variable is not supported on a trait only on a class

Category: E07xxx Full resolution · Reported in phase: FULL_RESOLUTION · Classification: TRAIT_BY_IDENTIFIER_NOT_SUPPORTED · All error codes

What went wrong

You're using 'by' delegation syntax in a TRAIT, but delegation only works in classes.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 770 (Why can't I use 'by' delegation inside a trait?)

How to fix it

  1. Use delegation in a class instead
  2. Traits define interfaces only
  3. Classes provide implementations via delegation

Common causes

  • Treating trait like a class
  • Trying to delegate trait implementation within trait
  • Misunderstanding what traits can contain

See also

  • E07040: 'by' delegation in trait (this error)
  • E07030: default operator in trait
  • E07070: constructor in trait

Examples

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

Fails with E07040: 'by' variable is not supported on a trait only on a class

#!ek9
defines module error.e07040.bad

  defines trait
    Reader
      read()
        <- rtn as String: "read"

    Writer
      write()
        -> text as String
        <- rtn as Boolean: text?

    //A trait has no properties, so it has nothing to delegate 'by'
    ReaderWriter with trait of Reader by reader, Writer

Fixed: Delegate 'by' a property in a class

#!ek9
defines module error.e07040.good

  defines trait
    Reader
      read() as abstract
        <- rtn as String?

  defines class
    FileReader with trait of Reader
      override read()
        <- rtn as String: "read from file"

    //A class can delegate a trait 'by' one of its properties
    Document with trait of Reader by reader
      reader as Reader: FileReader()

      default operator ?

Questions and answers

These Q&A entries show E07040 and its fix:

Found a problem?

If EK9 reported E07040 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.