tags:

views:

99

answers:

3

Here's a simplification of the classes I have:

trait RequiredThings {
  val requiredThings: Seq[String]
}

class SimpleCalculator with RequiredThings {
  val requiredThings = List("a", "b")
}

class ComplicatedCalculator extends SimpleCalculator with RequiredThings {
  self: SimpleCalculator =>
  override val requiredThings:List[String] = List("c") ::: self.requiredThings
}

In this version, I'm using a self-type annotation, but I'm not completely positive that's the way to go. I think I could probably get it to work by converting requiredThings to a method throughout, but I'd like to try it as a field.

Final solution:

trait RequiredThings {
  def requiredThings: Seq[String]
}

class SimpleCalculator with RequiredThings {
  def requiredThings = List("a", "b")
}

class ComplicatedCalculator extends SimpleCalculator with RequiredThings {
  override def requiredThings:List[String] = List("c") ::: super.requiredThings
}
+3  A: 

It makes sense to have requiredThings as a def at least in the trait - this will give you more flexibility

oxbow_lakes
That makes a lot of sense. I've done so in my solution.
scompt.com
+4  A: 

Funny enough, I stepped on the same issue yesterday.

The way self types are implemented currently, if you override a method (or value) there's no way to refer to the overridden method.

The following will loop:

class A { def foo = "a" }
trait B { self: A => override def foo = self.foo + "b" }

(note: you cannot call super.foo, the parent of B is ScalaObject, not A)

I also tried with one more indirection, but the program still loops.

class A { def foo = "a"; def fooA = A.this.foo }
trait B { this: A => override def foo = fooA + "b" }

There is work going on currently on self types, maybe this will change.

Lukas Rytz
+6  A: 

Yes, super calls to methods "inherited" through the self-type are not yet implemented. This will change (somewhat) soon. In the meantime, you should use inheritance instead.

lindydonna
This is what I ended up doing. I've updated the question with the final version of my code.
scompt.com
"This will change (somewhat) soon". I don't remember from the mailing-list where anyone said that this was likely to change. Could you show me where you got that info?Thanks.
Eric
@Eric: not sure if this is was ever posted on the mailing list, but I'm on the Scala team, and Martin has ok'd (in principle) such a feature. I just got bogged down with other things in the meantime, and there are some non-trivial implementation issues.
lindydonna