E06160: not an immediate trait of this context

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

What went wrong

You're trying to access a trait method using trait prefix syntax (TraitName.method), but the trait isn't an IMMEDIATE trait of this class. The trait is inherited indirectly through a parent class.

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. Call inherited method directly without trait prefix
  2. If you need explicit trait dispatch, add trait to immediate list
  3. Override in your class if you need specific behavior

Common causes

  • Using TraitName.method() for trait inherited from parent
  • Trying to access grandparent trait method directly
  • Assuming all inherited traits can use prefix syntax

See also

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

Examples

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

Fails with E06160: not an immediate trait of this context

#!ek9
defines module error.e06160.bad

  defines trait

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

    T1 extends T2
      anotherMessage() as pure
        <- rtn as String: "T1"

  defines class

    //Declares T1, which extends T2: only T1 is an immediate trait
    ViaSuperTrait with trait of T1

      describe()
        <- rtn as String?
        first <- T2.simpleMessage()
        second <- T1.anotherMessage()
        rtn: first + second

    //Declares no trait at all
    NoTrait

      describe()
        <- rtn as String?
        rtn: T2.simpleMessage()

Fixed: Qualify a call only with a trait the class names itself; an inherited method is just called

#!ek9
defines module error.e06160.good

  defines trait

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

    T1 extends T2
      anotherMessage() as pure
        <- rtn as String: "T1"

  defines class

    ViaSuperTrait with trait of T1

      describe()
        <- rtn as String?
        first <- simpleMessage()
        second <- T1.anotherMessage()
        rtn: first + second

    DirectTrait with trait of T2

      describe()
        <- rtn as String: T2.simpleMessage()

Questions and answers

These Q&A entries show E06160 and its fix: