Hawk66 Hawk66 - 1 year ago 85
Scala Question

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

The GenMapFactory class has an

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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download