E07270: trait methods cannot be marked with an access modifier, they are always public

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

What went wrong

You've added an access modifier (public/private/protected) to a trait method. Trait methods are always public by definition.

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?)

Rationale

Traits define public contracts. Implementation details should be in the implementing classes, not the traits.

How to fix it

  1. Remove access modifier from trait methods
  2. Move private implementation to implementing class

Common causes

  • Habit from class design
  • Trying to hide trait implementation details

See also

  • E07270: access modifier on trait method (this error)
  • E07280: unnecessary 'public' modifier

Examples

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

Fails with E07270: trait methods cannot be marked with an access modifier, they are always public

#!ek9
defines module error.e07270.bad

  defines trait

    Formatter
      format()
        -> amount as Integer
        <- rtn as String: prefix() + $amount

      //Every trait method is public
      private prefix()
        <- rtn as String: "#"

Fixed: Remove the access modifier

#!ek9
defines module error.e07270.good

  defines trait

    Formatter
      format()
        -> amount as Integer
        <- rtn as String: prefix() + $amount

      prefix()
        <- rtn as String: "#"

Questions and answers

These Q&A entries show E07270 and its fix: