How to pass Scala array into Scala vararg method?
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.
It is simple:
def test(some:String*){}def call () { val some = Array("asd", "zxc") test(some: _*)}
Starting Scala 2.13.0, if you use some: _*
, you will get this warning:
Passing an explicit array value to a Scala
varargs
method isdeprecated (since2.13.0
) and will result in a defensive copy; Usethe more efficient non-copyingArraySeq.unsafeWrapArray
or an explicittoIndexedSeq
call
As suggested, use scala.collection.immutable.ArraySeq.unsafeWrapArray
:
unsafeWrapArray(some):_*
Also, another warning that you should be getting is this one
procedure syntax is deprecated: instead, add
: Unit =
to explicitlydeclaretest
's return type
To fix that, add =
just before function's opening bracket:
def test(some:String*) = {