Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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?

like image 925
deltanovember Avatar asked Dec 28 '22 15:12

deltanovember


1 Answers

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 = ...
  }
}
like image 88
dfreeman Avatar answered Jan 12 '23 18:01

dfreeman