Implement Methods

Implement Methods adds an override stub for every abstract trait method a class has not yet implemented. Here C6 declares trait T1, which requires tMethod() and operator +; C6 has overridden the operator but not tMethod(), so EK9 refuses it - E07130, explained in the Hovers panel. To play it in the IDE: Help > Lessons > Refactoring…, or ek9 -ide -lesson implement-methods.

The EK9 IDE as the lesson opens: the code to read and, in the Hovers panel, its error explained

Start from

The code the lesson starts from: copy it into a file of your own and follow the steps.

Fails with E07130: not declared abstract but still has abstract methods/operators

#!ek9
defines module error.e07130.bad

  defines trait

    //Create a trait with one concrete method and some abstract methods/operators
    T1
      concreteMethod()
        -> count as Integer
        <- rtn as Boolean: count?

      tMethod()
        -> amount as Float
        <- rtn as Boolean?

      operator + as pure
        -> amount as Float
        <- rtn as Float?

  defines class
    //This is valid as a method is abstract and so this class is also abstract
    C1 as abstract
      default C1()

      aMethod() as abstract
        -> count as Integer
        <- rtn as Boolean?

      //A concrete method
      bMethod()
        -> count as Integer
        <- rtn <- true

      //Note that in class, we have to state that this is abstract (unlike traits)
      operator - as pure abstract
        -> amount as Float
        <- rtn as Float?

    //This too is valid
    C2 with trait of T1 as abstract
      default C2()

    //But this is Ok because it marked as abstract
    C3 extends C1 as abstract
      default C3()

    //This is an error, just marking as open is not enough, this is abstract
    //But it is picked up in the SYMBOL_DEFINITION phase and is checked elsewhere.
    //So it is commented out here - but left in place to highlight it is not missing
    <!-
    C4 as open
      default C4()

      aMethod() as abstract
        -> count as Integer
        <- rtn as Boolean?
    -!>

    //This is an error, this is abstract because T1.tMethod is abstract
    C5 with trait of T1
      default C5()

    //This is an error, this is abstract because T1.tMethod is abstract and marking as open makes not difference.
    //So even though the operator is now implemented, this not enough.
    C6 with trait of T1 as open
      default C6()

      override operator + as pure
        -> amount as Float
        <- rtn as Float: amount

    //Another error because C1 is abstract and C7 has not implemented and so must also be marked as abstract.
    C7 extends C1
      default C7()

    //So still an error because the operator is still unimplemented.
    C8 extends C1
      default C8()

      //We now override and implement
      override aMethod()
        -> count as Integer
        <- rtn as Boolean: true

    //So still an error because the aMethod is still unimplemented.
    C9 extends C1
      default C9()

      //We now override and implement
      override operator - as pure
        -> amount as Float
        <- rtn as Float: 0.0

    //Now this will be OK, as everything is implemented.
    C10 extends C1
      default C10()

      override aMethod()
        -> count as Integer
        <- rtn as Boolean: true

      override operator - as pure
        -> amount as Float
        <- rtn as Float: 0.0

    //This is now OK because all abstract method are implemented.
    C11 with trait of T1
      default C11()

      override tMethod()
        -> amount as Float
        <- rtn as Boolean: true

      override operator + as pure
        -> amount as Float
        <- rtn as Float: amount

Step 1: Where it is

Put the caret on C6, on line 65.

The EK9 IDE once this is done: Put the caret on C6, on line 65

Step 2: Implement Methods

Right-click it - or choose Refactor > Refactor This… - and choose Implement Methods…

The EK9 IDE once this is done: Right-click it - or choose Refactor > Refactor This… - and choose Implement Methods…

Step 3: Preview and apply

The preview shows every change the refactoring makes; then the IDE formats each file it changed. Choose Apply.

The result

That is Implement Methods: C6 now has an override stub for tMethod(), so every obligation trait T1 asks of it is met. C5, C7, C8 and C9 are left - this tool batches a trait's METHODS only, so an outstanding operator (Add Operator) or an abstract method inherited from a superclass needs a different fix. It is on the right-click menu wherever the caret is inside a class that directly declares a trait with an outstanding method; AI assistants use the ek9_implement_methods tool.

The EK9 IDE when the lesson has finished

Where this lesson comes from