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

SortedSet.collect does not accept a function for which there is no Ordering on the codomain #11019

Closed
MasseGuillaume opened this issue Jul 12, 2018 · 4 comments

Comments

@MasseGuillaume
Copy link

2.12

case class A(v: Int)
collection.immutable.SortedSet(1).collect{ case x => A(x) }
// collection.immutable.Set[A] = Set(A(1))

2.13

[error] /home/gui/scala/test/junit/scala/Playground.scala:12: No implicit Ordering defined for A.
[error]     collection.immutable.SortedSet(1).collect{ case x => A(x) }
[error]                                              ^
@MasseGuillaume
Copy link
Author

MasseGuillaume commented Jul 12, 2018

Hum, this is probably due to the absence of CanBuildFrom.

For map, I think the behavior in 2.13 make sense:

2.12

immutable.SortedSet(1).map(x => x)
// collection.immutable.SortedSet[Int] = TreeSet(1)

immutable.SortedSet(1).map(x => A(x))
// collection.immutable.Set[A] = Set(A(1))

2.13

[error] /home/gui/scala/test/junit/scala/Playground.scala:12: No implicit Ordering defined for A.
[error]     collection.immutable.SortedSet(1).map(x => A(x))
[error]                                          ^

@MasseGuillaume
Copy link
Author

MasseGuillaume commented Jul 12, 2018

This is documented here: https://github.com/scala/collection-strawman/wiki/FAQ#what-are-the-breaking-changes.

workarround:

val temp: Set[Int] = collection.immutable.SortedSet(1).
temp.collect{ case x => A(x) }

@SethTisue SethTisue added this to the 2.13.0-M5 milestone Jul 12, 2018
@julienrf
Copy link

julienrf commented Jul 13, 2018

What should we do @MasseGuillaume?

This is a documented change (and I would argue that the new behavior is a feature) and the recommended way to achieve what you want to achieve in the first post is to use the unsorted operation:

case class A(v: Int)
collection.immutable.SortedSet(1).unsorted.collect{ case x => A(x) }
// collection.immutable.Set[A] = Set(A(1))

@julienrf julienrf changed the title SortedSet.collect is too strict SortedSet.collect do not accept a function for which there is no Ordering on the codomain Jul 13, 2018
@julienrf julienrf changed the title SortedSet.collect do not accept a function for which there is no Ordering on the codomain SortedSet.collect does not accept a function for which there is no Ordering on the codomain Jul 13, 2018
@MasseGuillaume
Copy link
Author

unsorted is only available in 2.13: scala/scala-collection-compat#95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants