The App trait can be used to quickly turn objects into executable programs.
Here is an example:
Here, object Main inherits the main method of App .
args returns the current command line arguments as an array.
Caveats
It should be noted that this trait is implemented using the DelayedInit
functionality, which means that fields of the object will not have been
initialized before the main method has been executed.*
It should also be noted that the main method should not be overridden: the
whole class body becomes the “main method”.
Future versions of this trait will no longer extend DelayedInit .
The init hook. This saves all initialization code for execution within main .
This method is normally never called directly from user code. Instead it is
called as compiler-generated code for those classes and objects (but not traits)
that inherit from the DelayedInit trait and that do not themselves define a
delayedInit method.
body
the initialization code to be stored for later execution
Definition Classes
App → DelayedInit
Annotations
@ deprecated
Deprecated
(Since version 2.11.0) The delayedInit mechanism will disappear.
(defined at scala.App)
Value Members From scala.App
def main(args: Array[String]): Unit
The main method. This stores all arguments so that they can be retrieved with
args and then executes all initialization code segments in the order in which
they were passed to delayedInit .
args
the arguments passed to the main method
Annotations
@ deprecatedOverriding (message = “main should not be overridden”, since =
“2.11.0”)
(defined at scala.App)
Full Source:
Interested in Scala?
I send out weekly, personalized emails with articles and conference talks.