Skip to content

Make the breakOut rewrite rule cross compatible #80

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

Closed
julienrf opened this issue Jul 2, 2018 · 7 comments
Closed

Make the breakOut rewrite rule cross compatible #80

julienrf opened this issue Jul 2, 2018 · 7 comments

Comments

@julienrf
Copy link
Contributor

julienrf commented Jul 2, 2018

Do you think it’s possible, @MasseGuillaume?

@julienrf julienrf added this to the Week 27 milestone Jul 2, 2018
@MasseGuillaume
Copy link
Contributor

Not as is.

package fix

import scala.collection.compat._

object BreakoutSrc {
  val xs = List(1, 2, 3)

  xs.iterator.collect{ case x => x }.to(implicitly): Set[Int]
  xs.iterator.flatMap(x => List(x)).to(implicitly): collection.SortedSet[Int]
  xs.iterator.map(_ + 1).to(implicitly): Set[Int]
  xs.reverseIterator.map(_ + 1).to(implicitly): Set[Int]
  xs.iterator.scanLeft(0)((a, b) => a + b).to(implicitly): Set[Int]
  xs.iterator.concat(xs).to(implicitly): Set[Int]
  xs.view.updated(0, 1).to(implicitly): Set[Int]
  xs.iterator.zip(xs).to(implicitly): Map[Int, Int]
  xs.iterator.zipAll(xs, 0, 0).to(implicitly): Array[(Int, Int)]

  (xs.iterator ++ xs).to(implicitly): Set[Int]
  (1 +: xs.view).to(implicitly): Set[Int]
  (xs.view :+ 1).to(implicitly): Set[Int]
  (xs ++: xs.view).to(implicitly): Set[Int]
}
[info] Compiling 1 Scala source to /home/gui/scala-collection-compat/scalafix/output212/target/scala-2.12/classes...
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:16: value concat is not a member of Iterator[Int]
[error]   xs.iterator.concat(xs).to(implicitly): Set[Int]
[error]               ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:16: ambiguous implicit values:
[error]  both value StringCanBuildFrom in object Predef of type => scala.collection.generic.CanBuildFrom[String,Char,String]
[error]  and method $conforms in object Predef of type [A]=> A <:< A
[error]  match expected type T
[error]   xs.iterator.concat(xs).to(implicitly): Set[Int]
[error]                             ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:18: type mismatch;
[error]  found   : List[Int]
[error]  required: Iterator[?]
[error]   xs.iterator.zip(xs).to(implicitly): Map[Int, Int]
[error]                   ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:18: ambiguous implicit values:
[error]  both value StringCanBuildFrom in object Predef of type => scala.collection.generic.CanBuildFrom[String,Char,String]
[error]  and method $conforms in object Predef of type [A]=> A <:< A
[error]  match expected type T
[error]   xs.iterator.zip(xs).to(implicitly): Map[Int, Int]
[error]                          ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:19: type mismatch;
[error]  found   : List[Int]
[error]  required: Iterator[?]
[error]   xs.iterator.zipAll(xs, 0, 0).to(implicitly): Array[(Int, Int)]
[error]                      ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:19: ambiguous implicit values:
[error]  both value StringCanBuildFrom in object Predef of type => scala.collection.generic.CanBuildFrom[String,Char,String]
[error]  and method $conforms in object Predef of type [A]=> A <:< A
[error]  match expected type T
[error]   xs.iterator.zipAll(xs, 0, 0).to(implicitly): Array[(Int, Int)]
[error]                                   ^
[error] 6 errors found
[error] (scalafixOutput212/compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed Jul 2, 2018 4:14:04 PM

@julienrf
Copy link
Contributor Author

julienrf commented Jul 2, 2018

It looks like we should add concat, zip and zipAll to Iterator in IteratorExtensionMethods. Can you try that and see if there are still compilation errors?

@MasseGuillaume
Copy link
Contributor

[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:18: type mismatch;
[error]  found   : List[Int]
[error]  required: Iterator[?]
[error]   xs.iterator.zip(xs).to(implicitly): Map[Int, Int]
[error]                   ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:18: ambiguous implicit values:
[error]  both value StringCanBuildFrom in object Predef of type => scala.collection.generic.CanBuildFrom[String,Char,String]
[error]  and method $conforms in object Predef of type [A]=> A <:< A
[error]  match expected type T
[error]   xs.iterator.zip(xs).to(implicitly): Map[Int, Int]
[error]                          ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:19: type mismatch;
[error]  found   : List[Int]
[error]  required: Iterator[?]
[error]   xs.iterator.zipAll(xs, 0, 0).to(implicitly): Array[(Int, Int)]
[error]                      ^
[error] /home/gui/scala-collection-compat/scalafix/output212/src/main/scala/fix/cbf.scala:19: ambiguous implicit values:
[error]  both value StringCanBuildFrom in object Predef of type => scala.collection.generic.CanBuildFrom[String,Char,String]
[error]  and method $conforms in object Predef of type [A]=> A <:< A
[error]  match expected type T
[error]   xs.iterator.zipAll(xs, 0, 0).to(implicitly): Array[(Int, Int)]
[error]                                   ^
[error] four errors found
[error] (scalafixOutput212/compile:compileIncremental) Compilation failed

@julienrf
Copy link
Contributor Author

julienrf commented Jul 5, 2018

It seems that it’s not possible with the current version of scalafix.

@julienrf julienrf removed this from the Week 27 milestone Jul 5, 2018
@MasseGuillaume
Copy link
Contributor

@julienrf what if we do this:

xs.iterator.zip(xs).to(Map[Int, Int])

Do we need to make sure the parameter of to has an instance of Factory?

@julienrf
Copy link
Contributor Author

julienrf commented Jul 5, 2018

It would rather be:

xs.iterator.zip(xs).to(Map)

This means that we need to find a value that is (or can be converted to) a Factory of the expected type.

@julienrf
Copy link
Contributor Author

julienrf commented Jul 5, 2018

In most of the cases I guess people use breakOut with a concrete expected collection type (e.g. List[Int], Map[Int, String]) so it might be possible to find this type and make the assumption that there exists a corresponding object (with the same symbol) that is a Factory (or can be converted to).

But cases like the following won’t work:

def foo[C](implicit cbf: CanBuildFrom[Nothing, Int, C]): C = {
  val xs = List(1, 2, 3)
  xs.map(_ + 1)(collection.breakOut) // the expected type is `C`
}

MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 18, 2018
MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 18, 2018
MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 18, 2018
MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 19, 2018
MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 20, 2018
MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 20, 2018
MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 23, 2018
MasseGuillaume added a commit to MasseGuillaume/scala-collection-compat that referenced this issue Jul 23, 2018
julienrf added a commit that referenced this issue Jul 23, 2018
Make the breakOut rewrite rule cross compatible (fix #80) (fix #93)
martijnhoekstra pushed a commit to martijnhoekstra/scala-collection-compat that referenced this issue Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants