Philip Nguyen
Philip Nguyen

Reputation: 891

(Scala) How to convert List into a Seq

I have a function like

def myFunction(i:Int*) = i.map(a => println(a))

but I have a List of Int's.

val myList:List[Int] = List(1,2,3,4)

Desired output:
1
2
3
4

How can I programmatically convert myList so it can be inserted into myFunction?

Upvotes: 2

Views: 1771

Answers (1)

insan-e
insan-e

Reputation: 3922

Looking at your desired input and output, you want to pass a List where a varargs argument is expected.
A varargs method can receive zero or more arguments of same type.
The varargs parameter has type of Array[T] actually.
But you can pass any Seq to it, by using "varargs ascription/expansion" (IDK if there is an official name for this):

myFunction(myList: _*)

Upvotes: 4

Related Questions