Reputation: 17933
I am writing a trait that contains a function:
trait MyTrait[X,Y] {
def f(x : X) : Y
}
I'm trying to add a map function:
trait MyTrait[X,Y] {
def f(x : X) : Y // the outer f
def map[Z](g : Y => Z) : MyTrait[X,Y] = MyTrait[X,Z] {
//namespace conflict : I need to access the outer f within this f
override def f(x : X) : Z = outerF andThen g
}
}
My question is: how do I reference the outer f
? I've tried super.f
and this.f
to no avail. I'm sure this is a basic question that has been asked before but it's not very "google-able", e.g. easy to search for.
thank you in advance for your consideration and response.
Upvotes: 1
Views: 47
Reputation: 170733
You can also use override def f(x : X) : Z = MyTrait.this.f andThen g
.
Upvotes: 1
Reputation: 14825
You can use self
type
trait MyTrait[X,Y] {
self =>
def f(x : X) : Y // the outer f
def map[Z](g : Y => Z) : MyTrait[X,Y] = MyTrait[X,Z] {
override def f(x : X) : Z = self.f andThen g
}
}
Upvotes: 2