Scala trait composition issue

I have a class which source I cannot modify:

class Foo {

  def bar() = println("bar")

}

And a trait I'd like to mix into it at runtime

trait Zee { this: Foo =>

  abstract override def bar() = {
    println("before bar")
    super.bar()
  }
}

This is throwing that bar is not a member of Object with ScalaObject

What am I doing wrong? Is it possible to achieve this without modifying Foo source?

The ultimate client code needs to look like this:

val foo = new Foo with Zee
foo.bar() // should print 'before bar' and then 'bar'

Answers


Your Zee trait has no super traits (except implicit inheritance from ScalaObject) thus super does not contain definition for bar and there is nothing to override or call (super.bar).

why don't you write this without self-reference?

class Foo {

  def bar() = println("bar")

}


trait Zee extends Foo {

  abstract override def bar() = {
    println("before bar")
    super.bar()
  }
}

You can just extends the class Foo in your trait:

trait Zee extends Foo

And your code will work.


your trait needs to extend foo

trait Zee extends Foo {
  abstract override def bar() = {
    println("before bar")
    super.bar()
  }
}

Need Your Help

How to Stop Gmail from maximizing an iFrame?

javascript jquery iframe gmail

I am trying to create a webpage with Gmail embedded in an iframe. However, Gmail has some javascript code that executes whenever it is loaded. I believe the following js code snippet is what causes...

Expected methods never called when running python mox test

python mox

I'm trying to write a mox test that reads a spreadsheet (4 columns), gets the feed and writes specific columns (2 columns) to a CSV file. I'm trying to get past the first step which is get the list...