Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value for variable arguments in scala method

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.

like image 623
Matroska Avatar asked Feb 21 '26 21:02

Matroska


1 Answers

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")
like image 186
Michael Zajac Avatar answered Feb 24 '26 11:02

Michael Zajac