Hawk66 Hawk66 - 2 months ago 17
Scala Question

What type is expected for (elems: (A, B)*) in GenMapFactory

The GenMapFactory class has an

apply
function, with the following signature

def apply[A, B](elems: (A, B)*): CC[A, B] = (newBuilder[A, B] ++= elems).result()


What kind of collection I'm supposed to use here ? I thought I could just use a sequence but that does not seem to work

Answer

The * indicates a variable length argument list (also known as varargs).

In the case of a Map you would use it like this:

val newMap: Map[A, B] = Map((a0, b0), (a1, b1), (a2, b2))

There is also a neater way to express the (key, value) syntax, like this:

val newMap: Map[A, B] = Map(a0 -> b0, a1 -> b1, a2 -> b2)
Comments