scala / scala-collection-compat

makes some Scala 2.13 APIs (primarily collections, also some others) available on 2.11 and 2.12, to aid cross-building
Apache License 2.0
203 stars 86 forks source link

Add `SortedSet.unsorted` and `SortedMap.unsorted` #95

Open MasseGuillaume opened 6 years ago

MasseGuillaume commented 6 years ago

SortedSet.map in 2.12 returns a Set if it cannot find an implicit Ordering. To keep this behavior in 2.13, you can use unsorted. However, this method is not available in 2.12.

case class A(v: Int)
val set = collection.immutable.SortedSet(1)
set.map(x => A(x))
// Set(A(1)): Set[A] in 2.12
// error: No implicit Ordering defined for A. in 2.13
julienrf commented 6 years ago

I think a cross-compatible workaround would be to explicitly upcast the sorted set: (TreeSet(1, 2): Set[Int]).map(f)

MasseGuillaume commented 6 years ago

I think this is cleaner:

import compat._
TreeSet(1, 2).unsorted.map(f)
MasseGuillaume commented 6 years ago

Unfortunately, this rewrite is going to be in the Experimental. Once again we hit https://github.com/scalameta/scalameta/issues/1212.

Sorted collection have a special map functions that takes an ordering. The fallback map is defined in, for example, SetLike.map. We cannot make the difference between BitSet(1).map(f) and Set(1).map(f) since we don't know the type of the lhs (BitSet(1): BitSet and Set(1): Set[Int]). I think the short term solution for us is to fork scalameta's semanticdb.

julienrf commented 6 years ago

I see. OK.

szeiger commented 5 years ago

Labeling as library. Adding the unsorted method in 2.12 should be the easiest solution for now.