Reputation: 15788
The method prototype for main is:
def main(args: Array[String]): Unit
Typically, an application needs to specify a return code when it exits. How is that typically done in scala if main returns Unit? Should I call System.exit(n)?
Also, the docs warn that I should not use main at all, though this seems at odds with the getting started guide).
What's the best practice here?
Upvotes: 14
Views: 13640
Reputation: 67290
Yes, you exit with a code different than zero by calling either java.lang.System.exit(n)
or better sys.exit(n)
(which is Scala's equivalent).
If you mix in App
in your main application object, you do not define method main
but can just write its contents in the object's body directly.
E.g.
object Test extends App {
val a0 = args.headOption.getOrElse {
Console.err.println("Need an argument")
sys.exit(1)
}
println("Yo " + a0)
// implicit: sys.exit(0)
}
Upvotes: 27