Phil
Phil

Reputation: 50436

I have a Scala List, how can I get a TraversableOnce?

Starting with a Scala list.

val list = List(1,2,3,4)

How can I convert it to a TraversableOnce?

Upvotes: 6

Views: 7056

Answers (2)

Guillaume
Guillaume

Reputation: 22822

list.iterator

would do the trick if you specifically need to iterate, but your list is already a TraversableOnce

http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List

Upvotes: 0

Ben James
Ben James

Reputation: 125207

You already have one, since List[A] is a subtype of TraversableOnce[A]. You should not need to do anything to convert it.

To verify this:

scala> implicitly[List[Int] <:< TraversableOnce[Int]]
res0: <:<[List[Int],TraversableOnce[Int]] = <function1>

Upvotes: 9

Related Questions