05 July 2009

Scala Koala: I'm a solitary main()

Scala main() must be in a standalone object by itself?

I was a bit surprised by this:

java.lang.NoSuchMethodException: HelloWorld.main([Ljava.lang.String;)

I thought I'd seen numerous examples in books and on the web of classes and objects defined with the same name in the same Scala source file. And I have!

So I thought I'd begin my nascent Scala library in a single file, with a main() driver function:

class STable {
val cols = List()
val rows = List()
}

object STable {
def main(args: Array[String]) {
val table = new STable()
}
}


Steps Book Says

On page 68 of Programming In Scala it says: To run a Scala program, you must supply the name of a standalone singleton object with a main method that takes one parameter, an Array[String], and has a result type of Unit.

Am I The Only One?

Others have struck the same problem:
  • John Tyler in his Nable post expresses a healthy response to the unexpected
  • And Wondering Around's Scala's main issue post is a little annoyed but positive about Scala

Bug or Feature?

I was relieved to find that it's actually a bug. Possibly edging its way into being a feature if it isn't fixed.

It's Scala Ticket #363 which is a reopened defect.

Bill Venners got it right for the book:

In which case I need to update the book because it is wrong. It should
say that any *standalone object* with a main of the proper signature
can be used as an entry point into an application. MyApp$ has a main
method, but it isn't static. I had never attempted to make an
application with anything other than a standalone object.

The book reflects the current reality.

I look forward to this defect being fixed so it doesn't catch the unwary unawares.

Followers