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*
.
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
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