greenoldman
greenoldman

Reputation: 21062

How to pass variadic arguments while adding another one in Scala?

Consider these methods:

def clearlnOut(coll : Any*)
{
  clearOut(coll:_*,"\n") // error
}
def clearOut(coll : Any*)
{
  ...

The compiler says:

error: no `: _*' annotation allowed here (such annotations are only allowed in arguments to *-parameters)

Now I am puzzled. It is clear case of using variadic arguments, so how to pass such augmented "collection" properly?

Upvotes: 5

Views: 616

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340713

Try this:

def clearlnOut(coll : Any*) {
  clearOut(coll ++ "\n")
}

UPDATE: much better version suggested by @Rex Kerr (see comment below):

def clearlnOut(coll : Any*) {
  clearOut((coll :+ "\n"): _*)
}

Upvotes: 6

Related Questions