Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using App trait and main method in scala

Tags:

scala

What is the difference between

object Application extends App {    println("Hello World") } 

and

object Application {     def main(args: Array[String]): Unit = {         println("Hello World");     } } 
like image 677
Ramesh Avatar asked Jul 26 '12 10:07

Ramesh


1 Answers

The App trait is a convenient way of creating an executable scala program. The difference to the main method altenative is (apart from the obvious syntactic differences) that the App trait uses the delayed initalization feature.

From the release notes for 2.9 (see http://www.scala-lang.org/old/node/9483 )

Objects inheriting the App trait instead make use of Scala 2.9’s delayed initialization feature to execute the whole body as part of an inherited main method.

Another new feature of the App scheme is that command line arguments are now accessible via the args value (which is inherited from trait App)

like image 64
Emil H Avatar answered Sep 22 '22 14:09

Emil H