deltanovember
deltanovember

Reputation: 44041

In Scala, how do I initialise abstract vals in traits?

Suppose I have the following trait with two abstract vals

trait Base {
  val startDate: java.util.Date
  val endDate: java.util.Date
}

I now have an abstract class extending the trait

abstract class MyAbstract extends Base ...

I now wish to instantiate the abstract class with a few other traits mixed in.

  def main(args: Array[String]) {
   new MyAbstract with MixIn1 with MixIn2
  }

How to I pass in concrete values for startDate and endDate?

Upvotes: 4

Views: 1421

Answers (1)

dfreeman
dfreeman

Reputation: 2834

Because MyAbstract is an abstract class, you can't instantiate it directly. You need to either subclass it explicitly, or create an instance of an anonymous subclass, e.g.

def main(args: Array[String]) {
  val myInstance = new MyAbstract with MixIn1 with MixIn2 {
    val startDate = ...
    val endDate = ...
  }
}

Upvotes: 10

Related Questions