Skip to content

Rewrite rule .to[X] => .to(X) is not cross-compatible for Map #101

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
MasseGuillaume opened this issue Jul 18, 2018 · 2 comments
Closed
Milestone

Comments

@MasseGuillaume
Copy link
Contributor

Currently, the rewrite will do the following patch

-List((1, 2)).to[Map]: Map[Int, Int]
+List((1, 2)).to(Map): Map[Int, Int]

This gives a type error since the to function returns an Iterable[(Int, Int)].

The signature of .to[X] is

def to[Col[_]](implicit cbf: CanBuildFrom[Nothing, A, Col[A @uV]]): Col[A @uV]

https://github.com/scala/scala/blob/90c94cc2ead0b7b1d9ba170da08d27504b2f0fa8/src/library/scala/collection/GenTraversableOnce.scala#L668

Because of the Col[A] it's not possible to use to in order to create a Map[Int, Int]. We should guard for similar types: BitSet, SortedMap, etc and convert to the expected type.

-List((1, 2)).to[Map]: Map[Int, Int]
+List((1, 2)).to(Map).toMap: Map[Int, Int]
@julienrf
Copy link
Contributor

We should not produce .to(Map).toMap. Just .toMap is enough. But there will be no .toXxx methods for BitSet or SortedMap… Those cases can not be rewritten.

@julienrf julienrf added this to the 2.13.0-M5 milestone Jul 21, 2018
@MasseGuillaume
Copy link
Contributor Author

It's not possible to write xs.to[Map]. The solution is to write Map.from(xs) and provide the from method in scala < 2.13.

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