# Constructing a new TraverseableOnce or IterableOnce from existing instance

**URL:** <https://users.scala-lang.org/t/constructing-a-new-traverseableonce-or-iterableonce-from-existing-instance/12167>\
**Category:** Question\
**Created:** [January 8, 2026, 9:25pm UTC](https://users.scala-lang.org/t/constructing-a-new-traverseableonce-or-iterableonce-from-existing-instance/12167 "2026-01-08T21:25:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwalcock](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kwalcock](https://users.scala-lang.org/u/kwalcock)\
**Post date:** [January 8, 2026, 9:25pm UTC](https://users.scala-lang.org/t/constructing-a-new-traverseableonce-or-iterableonce-from-existing-instance/12167/1 "2026-01-08T21:25:08Z")

</div>

I’m updating some older Scala code to higher versions and can’t figure out how to construct a new TraversableOnce (Scala 2.11, 2.12) or IterableOnce (2.13+) from an existing instance of the same type. I do have the “scala-collection-compat” library dependency configured. There’s a method

```scala
private def sampleWithoutReplacement[A, CC[X] <: TraversableOnce[X]](xs: CC[A], k: Int)(implicit cbf: CanBuildFrom[CC[A], A, CC[A]]): CC[A] = {

```

that is called with an argument that is also a TraversableOnce. The usual error message is

Cannot construct a collection of type TraversableOnce[A] with elements of type A based on a collection of type TraversableOnce[A].

There’s code in the method that used to create and populate the new instance like this

```scala
  // return collection of the right type
  val builder = cbf(xs)
  builder ++= reservoir
  builder.result()

```

which is why the cbf is needed, but it’s not working in updated Scala, not even with BuildFrom rather than CanBuildFrom.

Does someone know what magic is needed?

---

<div class="post-metadata">

**Author:** ![som-snytt](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@som-snytt](https://users.scala-lang.org/u/som-snytt)\
**Post date:** [January 8, 2026, 11:41pm UTC](https://users.scala-lang.org/t/constructing-a-new-traverseableonce-or-iterableonce-from-existing-instance/12167/2 "2026-01-08T23:41:38Z")

</div>

```auto
scala> List(42).iterableFactory.from(Vector(27))
[[syntax trees at end of typer]] // rs$line$3
package <empty> {
  final lazy module val rs$line$3: rs$line$3 = new rs$line$3()
  final module class rs$line$3() extends Object() { this: rs$line$3.type =>
    val res1: List[Int] =
      List.apply[Int]([42 : Int]*).iterableFactory.from[Int](
        Vector.apply[Int]([27 : Int]*))
  }
}

val res1: List[Int] = List(27)

```

Where the companion knows how to build it. (Just guessing.)

---

<div class="post-metadata">

**Author:** ![kwalcock](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kwalcock](https://users.scala-lang.org/u/kwalcock)\
**Post date:** [January 9, 2026, 1:42am UTC](https://users.scala-lang.org/t/constructing-a-new-traverseableonce-or-iterableonce-from-existing-instance/12167/3 "2026-01-09T01:42:22Z")

</div>

The new instance can probably be created with something like

cbf.toFactory(xs).fromSpecific(reservoir)

However, I cannot call the method sampleWithoutReplacement for lack of a cbf which is causing the error message. Perhaps there is an import that I’m missing.

---

<div class="post-metadata">

**Author:** ![kwalcock](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kwalcock](https://users.scala-lang.org/u/kwalcock)\
**Post date:** [January 9, 2026, 5:14am UTC](https://users.scala-lang.org/t/constructing-a-new-traverseableonce-or-iterableonce-from-existing-instance/12167/4 "2026-01-09T05:14:51Z")

</div>

Below is a better code sample. The issue is getting from the method `choice` to `sampleWithoutReplacement` with an implicit `BuildFrom`. The comiler complains

```auto
Cannot construct a collection of type IterableOnce[A] with elements of type A based on a collection of type IterableOnce[A].
[error] sampleWithoutReplacement(xs, 1).iterator.next

```

for the code

```scala
package sample

import scala.collection.BuildFrom

object IterableOnceUtils {

  def choice[A](xs: IterableOnce[A]): A = {
    sampleWithoutReplacement(xs, 1).iterator.next
  }

  def sampleWithoutReplacement[A, CC[X] <: IterableOnce[X]](xs: CC[A], k: Int)(implicit cbf: BuildFrom[CC[A], A, CC[A]]): CC[A] = {
    ???
  }
}

```

---

<div class="post-metadata">

**Author:** ![kwalcock](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kwalcock](https://users.scala-lang.org/u/kwalcock)\
**Post date:** [January 11, 2026, 12:15am UTC](https://users.scala-lang.org/t/constructing-a-new-traverseableonce-or-iterableonce-from-existing-instance/12167/5 "2026-01-11T00:15:32Z")

</div>

The solution might be to require that the initial call to choice() have a BuildFrom available and to pass it on to the next method. This seems to work:

```scala
package ai.lum.common

import scala.collection.BuildFrom

object IterableOnceUtils {

  def choice[A, CC[X] <: IterableOnce[X]](xs: CC[A])(implicit cbf: BuildFrom[CC[A], A, CC[A]]): A = {
    sampleWithoutReplacement(xs, 1)(cbf).iterator.next()
  }

  def sampleWithoutReplacement[A, CC[X] <: IterableOnce[X]](xs: CC[A], k: Int)(implicit cbf: BuildFrom[CC[A], A, CC[A]]): CC[A] = {
    ???
  }
}

```
