Skip to content

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

@MasseGuillaume

Description

@MasseGuillaume

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions