3

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.

1 Answer 1

5

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.