E07075: traits do not support properties/fields

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

What went wrong

You're defining a property/field in a TRAIT, but traits are stateless behaviour contracts and cannot have properties. Only classes, records, and components can hold state.

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?)
  • ek9 -q 105 (How does method dispatch work in EK9?)

How to fix it

  1. Remove property from trait
  2. Define abstract accessor methods instead
  3. Move state to implementing class

Common causes

  • Treating trait like a class with state
  • Trying to share default values across implementations
  • Misunderstanding trait purpose (behaviour, not state)

See also

  • E07070: constructor in trait
  • E07075: property in trait (this error)
  • E07030: default operator in trait

Examples

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

Fails with E07075: traits do not support properties/fields

#!ek9
defines module error.e07075.bad

  defines trait
    Nameable

      //A trait holds behaviour, never state
      personName <- "default"

Fixed: A trait holds behaviour, never state: ask for the value with an abstract method instead of a property

#!ek9
defines module error.e07075.good.fix

  defines trait
    Nameable

      //A trait holds behaviour, never state
      personName() as abstract
        <- rtn as String?

Fixed: Declare an abstract method on the trait, and the property on the class

#!ek9
defines module error.e07075.good

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

  defines class
    Person with trait of Nameable
      personName <- "default"

      override name()
        <- rtn as String: personName

      default operator ?

Questions and answers

These Q&A entries show E07075 and its fix:

Found a problem?

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