When debugging command line argument handling in Java I'm used to doing
args = new String[] { "some", "new", "arguments" };
(especially useful if have a filename as argument which you frequently change, but don't want to go through some dialog windows in the IDE). This has the benefit that I can simply comment out the line when building a release.
So when I tried this in Scala I discovered that arguments are val
s. (And I can't write var
in front of the parameter).
Q2: So is there any obvious work-around except for doing
val newArgs = if (...) args else Array("some", "new", "arguments")
and stick to newArgs
in the remaining main method?
Q1: Mutating the input parameters is often seen as bad style and makes it harder to reason about code.
Q2: You could assign the args
to a var
before doing anything with it.
Arrays are mutable, so if you insist:
Seq("some", "new", "arguments").copyToArray(args, 0, 3)
That does, of course, only work if there is enough space in the passed array.
Remember that you can use default parameters in Scala to solve your original probem in a much cleaner way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With