In my Scala application (ver 2.11), I want to use the main args all over my App.
In order to do so, I thought to create an object (which is singleton in Scala), and to init it with main-args
Something like this:
object MyMain{
def main(args: Array[String]):Unit = {
//how to set SingletonArgs with args???
}
object SingletonArgs{
def getArg0():String{...}
def getArg1():String{...}
}
class AnotherClass(){
def printArg0(){
println(SingletonArgs.getArg0)
}
}
How can I init the SingletonArgs?
Is there a different way to share the main args?
As Tim mentioned in his comment: don't do this.
If you don't want to clutter your code with these args, you can use implicits
:
object MyMain {
def main(args: Array[String]): Unit = {
implicit val myArgs = MyArgs(args(0), args(1))
new AnotherClass().printArg0()
}
}
case class MyArgs(arg0: String, arg1: String)
class AnotherClass()(implicit args: MyArgs) {
def printArg0() {
println(args.arg0)
}
}