Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Scala array into Scala vararg method?

Consider the code below:

private def test(some:String*){  }  private def call () {   val some = Array("asd", "zxc")   test(some) } 

It prints expect String, found Array[String] Why? Are Scala varargs not arrays?

Note

I found several questions on Stack Overflow about Scala varargs, but all of them are about calling Java varargs methods or about converting Scala lists to arrays.

like image 464
Cherry Avatar asked Jun 26 '15 04:06

Cherry


1 Answers

Append :_* to the parameter in test like this

test(some:_*) 

And it should work as you expect.

If you wonder what that magical :_* does, please refer to this question.

like image 126
Yuhuan Jiang Avatar answered Oct 04 '22 19:10

Yuhuan Jiang