Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IterableOnce.iterator not available in Scala 2.12.x #493

Closed
borice opened this issue Oct 22, 2021 · 5 comments
Closed

IterableOnce.iterator not available in Scala 2.12.x #493

borice opened this issue Oct 22, 2021 · 5 comments
Labels
good first issue Good for newcomers

Comments

@borice
Copy link

borice commented Oct 22, 2021

Scala 2.12:

val t: TraversableOnce[Int] = List(1,2,3)
val tm = t.map(_ + 1)

migrating the above contrived example to Scala 2.13 (according to the @deprecated instructions) leads to:

Scala 2.13:

import scala.collection.compat.IterableOnce

val t: IterableOnce[Int] = List(1,2,3)
val tm = t.iterator.map(_ + 1)  // since t.map was deprecated, requiring t.iterator.map

Trying to cross-compile the Scala 2.13 version to 2.12 using scala-collection-compat, we get a compilation error complaining about .iterator not being a member of TraversableOnce[Int]. Indeed, .iterator is only available on IterableOnce, but not in TraversableOnce. However, TraversableOnce does have a .toIterator method (but it's deprecated in IterableOnce).

Adding a .iterator extension method to TraversableOnce in scala-collection-compat for Scala 2.12- that calls .toIterator would solve the problem.

@SethTisue SethTisue added the good first issue Good for newcomers label Oct 28, 2022
@mcanlas
Copy link

mcanlas commented Jun 5, 2023

@mcanlas
Copy link

mcanlas commented Jun 6, 2023

I may have misspoke.

I don't believe the import as stated works. In order to get the implicit conversions that power this library, you would need a wildcard import.

import scala.collection.compat._

Having such an import makes the .iterator code work.

Moreover I believe the functionality already exists here https://github.com/scala/scala-collection-compat/blob/main/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala#L416

Voting to close this one as not an issue.

@SethTisue
Copy link
Member

@borice not sure if you're still around, but do you agree...?

@SethTisue
Copy link
Member

% scala-cli -S 2.12.18 --dep org.scala-lang.modules::scala-collection-compat:2.10.0
Welcome to Scala 2.12.18 (OpenJDK 64-Bit Server VM, Java 17.0.7).
Type in expressions for evaluation. Or try :help.

scala> import scala.collection.compat._
import scala.collection.compat._

scala> val t: IterableOnce[Int] = List(1,2,3)
t: collection.compat.IterableOnce[Int] = List(1, 2, 3)

scala> val tm = t.iterator.map(_ + 1)
tm: Iterator[Int] = <iterator>

scala> tm.toList
res0: List[Int] = List(2, 3, 4)

@SethTisue
Copy link
Member

@mcanlas thanks for looking into it!

@borice if you're seeking to avoid a wildcard import, the following set of imports is also sufficient:

import scala.collection.compat.IterableOnce
import scala.collection.compat.toTraversableOnceExtensionMethods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants