How to factor out main method of Scala objects into a shared location?

program-entry-point, refactoring, scala

Solution

I don't have Eclipse on this computer so I can't test if it works with Ctrl+F11, but I think you want a self-type:

trait Main {
  self: Test => 
  def main(args: Array[String]) {
    runTests
  }
}

You then just mix it in after your `Test` trait:

object BoardModelTest extends Test with TestUtils with Main {
  def runTests {}
}

Problem

In several Scala objects I have defined a main method that invokes runTests which is an abstract method in Test. Is there a way for the main method to be factored out into a common location (Trait or other solution) so that I can still run the test in Eclipse by keying ctrl-F11? This is what I have at present, https://github.com/janekdb/stair-chess/blob/master/src/chess/model/BoardModelTest.scala ``` object BoardModelTest extends Test with TestUtils { def main(args: Array[String]) { runTests } def runTests { ... ``` https://github.com/janekdb/stair-chess/blob/master/src/test/Test.scala ``` trait Test { def runTests: Unit ... ```

Original source