E06170: trait method access not supported here

Category: E06xxx Type hierarchy checks · Reported in phase: FULL_RESOLUTION · Classification: TRAIT_ACCESS_NOT_SUPPORTED · All error codes

What went wrong

You're using trait method access syntax (TraitName.method) outside of a class that implements that trait. This syntax is only valid inside classes that directly implement the trait.

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. Add 'with trait of MyTrait' to your class if needed
  2. Only use TraitName.method() syntax inside implementing class
  3. Create instance of trait-implementing class if calling from outside

Common causes

  • Using trait prefix in wrong class
  • Using trait prefix in standalone function
  • Forgetting to add trait to class's trait list

See also

  • E06170: trait access outside implementing class (this error)
  • E06160: trait not immediate to this class (inherited indirectly)

Examples

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

Fails with E06170: trait method access not supported here

#!ek9
defines module error.e06170.bad

  defines trait

    T2
      simpleMessage() as pure
        <- rtn as String: "T2"

  defines class

    //Even in a class with the trait, dynamic functions can't access it
    InvalidT2AccessFromDynamicFunctionInClass with trait of T2

      NotAllowedFromDynamicClass()
        var <- String()
        someFunction <- () is Acceptor of String as function (t :=: T2.simpleMessage())
        someFunction(var)

  defines function

    //Trait method call from standalone function - not allowed
    SomeFunction()
      var <- T2.simpleMessage()
      require var?

    //Dynamic function trying to access trait - not allowed
    TestDynamicFunction()
      var <- String()

      someFunction <- () is Acceptor of String as function (t :=: T2.simpleMessage())
      someFunction(var)

Fixed: Call trait methods only where the trait is an immediate trait: a class, or a dynamic class that names it

#!ek9
defines module error.e06170.good

  defines constant
    MaximumLength <- 300

  defines trait

    T2
      simpleMessage() as pure
        <- rtn as String: "T2"

    LengthValidator
      validateLength() as abstract
        -> text as String
        <- valid as Boolean?

  defines function

    validDynamicClass()
      <- rtn as Boolean?
      validator <- () with trait of LengthValidator, T2 as class
        override validateLength()
          -> text as String
          <- valid as Boolean: false
          if text?
            message <- `${T2.simpleMessage()}: ${text}`
            valid: length message < MaximumLength

      rtn: validator.validateLength("Steve")

Questions and answers

These Q&A entries show E06170 and its fix: