E07070: traits do not support constructor methods

Category: E07xxx Full resolution · Reported in phase: SYMBOL_DEFINITION · Classification: TRAITS_DO_NOT_HAVE_CONSTRUCTORS · All error codes

What went wrong

You're defining a constructor in a TRAIT, but traits cannot have constructors - only classes can create instances.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 106 (What are traits and how do I use them in EK9?)

How to fix it

  1. Remove constructor from trait
  2. Use default values for initialization
  3. Move construction logic to implementing class

Common causes

  • Treating trait like a class
  • Trying to initialize trait state
  • Misunderstanding trait capabilities

See also

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

Examples

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

Fails with E07070: traits do not support constructor methods

#!ek9
defines module error.e07070.bad

  defines trait
    Nameable

      //A trait is never constructed on its own
      Nameable()

Fixed: Construct the class that has the trait

#!ek9
defines module error.e07070.good

  defines trait
    Nameable
      name() as abstract
        <- rtn as String?

  defines class
    Person with trait of Nameable
      personName as String?

      default private Person()

      Person()
        -> newName as String
        this.personName: newName

      override name()
        <- rtn as String: personName

      default operator ?

Questions and answers

These Q&A entries show E07070 and its fix:

Found a problem?

If EK9 reported E07070 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.