0

Given this code: (no matter whether it doesn't make much sense)

    object Test {

          def main(args: Array[String]) {
           (new FooImpl2()).foo()
          }

          trait Foo {
            def foo()
          }

          trait M extends Foo {
            abstract override def foo() {println("M"); super.foo()}
          }

          abstract class FooImpl1 extends Foo {

          }

          class FooImpl2 extends FooImpl1 with M{
            override def foo() {println("Impl2")}
          }

    }

At compile time, this error occurs:

error: overriding method foo in trait M of type ()Unit;
method foo needs `abstract override' modifiers
override def foo() {println("Impl2")}

So at this place:

class FooImpl2 extends FooImpl1 with M{
    override def foo() {println("Impl2")}
}

Why doesn't override apply on FooImpl1 (in order to provide a concrete implementation for abstract trait method)? It seems like it matches the trait method's instead...and obviously there's a huge conflict with the pattern "abstract override"

1 Answer 1

1

M needs to be mixed-in after the concrete def (in linearization order).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) So that explains why this code works: class FooImpl1 extends Foo { def foo() {println("Impl")} } class FooImpl2 extends FooImpl1 with M{ override def foo() {println("Impl2")} } since concrete method implementation is defined before M is mixed-in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.