Quantcast
Channel: Rants, Raves and Ridicule
Viewing all articles
Browse latest Browse all 25

Avoid the Applicaiton trait in Scala

$
0
0
All, It's been a *long* time since I've been writing this blog. Today I felt I'd try for a very short article.

It seems to me that about every 2 weeks, someone comes on the Scala #irc or mailing lists and asks about performance issues. It also appears there is no end to people who want to use Scala's Application trait.

Do not be so tempted.

The application trait is a very neat feat of scala-mojo for automagically creating a main method, however it has one serious flaw:

Your entire application is run inside a static initializer block.

This having another serious flaw:

Hotspot will not optimize your application.

Anyone who wishes to have a real "scala" application like trait and doesn't want to write an ugly
def main(args : Array[String]) : Unit = {}
should run down to their local github and look at DRMacIver's Optional library.

Update: You should look at paulp's github fork of optional. I was unaware of this before I wrote the blog article. I've modified appropriately

You still have to write a main method, but this team the arguments *mean* something, not just to the compiler, but to the command line!

Here's the readme from paulp's Optional repo:


optional is a command line option parser and library.

YOU WRITE:

object MyAwesomeCommandLineTool extends optional.Application {
// for instance...
def main(count: Option[Int], file: Option[java.io.File], arg1: String) {
[...]
}
}

THEN YOU DO:

scala MyAwesomeCommandLineTool --count 5 quux

AND YOUR MAIN METHOD WILL BE INVOKED SUCH THAT:

count = Some(5)
file = None
arg1 = quux

See the example programs for many more features.

HOW IT WORKS:

Reflection, man.

CREDITS:

Idea and prototype implementation: DRMacIver.
Fleshing out and awesomification: paulp.




In Other news

I've recently been doing a bit of Scala + EJB and (funny enough) PowerShell scripting. Hopefully I'll have good things to write about and (more importantly) the time to do so in the near future.

Viewing all articles
Browse latest Browse all 25

Trending Articles