2

In Scala, suppose classes A and B that has a common method, but not inherited any common interface:

class A {
  def foo() {
    println("A.foo")
  }
}

class B {
  def foo() {
    println("B.foo")
  }
}

What I want to do is to define a trait that extends any classes that has foo() method. I tried this:

type Fooable = {
  def foo()
}

trait Q extends Fooable {
  override def foo() {
    println("before super.foo")
    super.foo()
    println("after super.foo")
  }
}

(new A with Q).foo()
(new B with Q).foo()    

However, Scala compiler rejects this code with an error:

error: class type required but AnyRef{def foo(): Unit} found trait Q extends Fooable {

The best solution is to make an interface that contains common methods and modify classes A and B to inherit the interface. Unfortunately, we cannot modify these classes. This is common in Java libraries such as:

  • java.sql.Connection.close() and java.io.Closeable.close()
  • android.app.Activity.onDestroy() and android.app.Service.onDestroy()

2 Answers 2

4

You can't do it. Here's the fundamental problem with it, quoting you:

define a trait that extends any classes

Scala demands that the class hierarchy to be a tree, which means any element in it will have one, and just one, parent class. It can have many parent traits, and many ancestor classes, but what you ask for is strictly against the hierarchy demanded by Scala.

Now, you are mixing two concepts here: nominal typing, where the type of something is defined by its class or trait, and structural typing, where the type of something is defined by what methods it implements.

Nominal typing and structural typing are, however, rather incompatible. In fact, before Scala it was generally agreed on that the two could not cooexist and, in fact, Scala has a very limited version of structural typing. You could do something like this:

scala> def f(m: { def foo(): Unit }) = {
     |     println("before foo")
     |     m.foo()
     |     println("after foo")
     | }
f: (m: AnyRef{def foo(): Unit})Unit

scala> f(new A)
before foo
A.foo
after foo

Or you could combine structural types with self types, like this:

scala> trait Q { self: { def foo(): Unit } =>
     |   def bar() {
     |     println("before foo")
     |     foo()
     |     println("after foo")
     |   }
     | }
defined trait Q

scala> (new A with Q).bar()
before foo
A.foo
after foo

scala> (new B with Q).bar()
before foo
B.foo
after foo

But you cannot inherit from a structural type, as inheritance is a characteristic exclusive of nominal types. And, because you cannot inherit a structural type, you cannot override it, or call it on an ancestor, which means trait Q cannot declare an override on foo().

0
0

Self answer:

After I know that there are no exact solution to my question, I made an alternative way to overcome this. It is not a perfect solution, but can be a reasonable workaround for some situation (in my case :-).

trait Fooable {
  def beforeFoo() {}
  def afterFoo() {}
}
trait Alice extends A with Fooable {
  override def foo() {
    beforeFoo()
    super.foo()
    afterFoo()
  }
}
trait Bob extends B with Fooable {
  override def foo() {
    beforeFoo()
    super.foo()
    afterFoo()
  }
}
trait Quest extends Fooable {
  override def beforeFoo() {
    println("before super.foo")
    super.beforeFoo()
  }

  override def afterFoo() {
    println("after super.foo")
    super.afterFoo()
  }
}

(new Alice with Quest).foo()
(new Bob with Quest).foo()

Yes, we should use Alice and Bob, instead of A and B. However, it can be compromised in my case.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.