Syntax for calling Scala variable length parameter from Java?

I have a Scala class whose constructor takes a variable length parameter list.

case class ItemChain(items: Item*)

      

From Scala it can be called like this

ItemChain(Item(), Item())

      

I can't figure out the syntax for calling it from Java. If i do this

new ItemChain(new Item(), new Item())

      

I am getting a compiler error that says this string does not match the signature scala.collection.seq<Item>

.

I can directly create a Scala sequence object from Java.

new scala.collection.Seq<Item>()

      

But I can't figure out how to add two instances to it afterwards Item

. If I build Java List

from Item

and drop it on scala.collection.Seq

, I get a runtime error.

+3


source to share


1 answer


This should do the trick:



import static scala.collection.JavaConverters.asScalaBufferConverter;
import static java.util.Arrays.asList;

...

new ItemChain(asScalaBufferConverter(asList(new Item(), new Item())).asScala());

      

+4


source







All Articles