I'm trying to create a trait that, when mixed in, will replace the default definition of a method with one that calls the original method and then manipulates the result.
Here's what I'm trying to do:
class Foo {
def bar() : String = "Foos bar"
}
trait OtherStuff {
self : Foo =>
def bar() : String = self.bar() + " with OtherStuff"
}
class Quux extends Foo with OtherStuff
If this worked the way I wanted it to, then (new Quux).bar
would now return Foos bar with OtherStuff
. Unfortunately, it doesn't work that way - what I get is:
<console>:6: error: error overriding method bar in class Foo of type ()String;
method bar in trait OtherStuff of type ()String needs `override' modifier
class Quux extends Foo with OtherStuff
But if I use override
when defining OtherStuff
, I get:
<console>:7: error: method bar overrides nothing
override def bar() : String = self.bar() + " with OtherStuff"
Is it possible to override a method in a self-type using trait? If not, will changing OtherStuff
to be a trait that extends Foo
instead of one that has a self-type of Foo
do anything bad to all the code that exists saying things like
class WhatEver extends Foo with Xyz with Pqr with OtherStuff with Abc
I'm working in scala 2.7.7 because this is an sbt build rule, and we haven't upgraded our sbt project to the 0.10.x versions yet. (Plugins we depend on aren't ready yet)