Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument with star to the next method?

Tags:

scala

scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) }
<console>:8: error: type mismatch;
  found   : String*
  required: String
    class A(s: String*)  {val l: ListBuffer[String] = ListBuffer[String](s)}

Why is it not possible to pass the argument s to the apply method of ListBuffer[String] which is

def apply[A](elems: A*): CC[A] = { ... }

(Method apply from the GenericCompanion.scala )

The code ListBuffer[String]("foo", "bar") does work. But it seems I can not pass through the argument list of strings from s which is also String*.

like image 786
John Threepwood Avatar asked Jun 22 '12 13:06

John Threepwood


1 Answers

You need to tell Scala to unpack s:

ListBuffer[String](s: _*)

You also don't need the the explicit types:

scala> class A (s: String*) { val l = ListBuffer(s: _*) }
defined class A
like image 97
dhg Avatar answered Sep 28 '22 01:09

dhg