I want to define a default value for a variable argument parameter in a Scala method. Is it possible?
def aMethod(variableArguments: String* = ???){}
I have tried using variableArguments:String* = Seq("a","b","c"):_* but it doesn't work.
The compiler error makes it pretty clear-cut:
:10: error: a parameter section with a `*'-parameter is not allowed to have default arguments
You can work around this with overloading, if you wish:
def test(a: String*): String = a.mkString
def test(): String = test("a", "b", "c")
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