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

Scala 3 Ambiguous Implicits in UnorderedFoldable #4370

Closed
bpholt opened this issue Jan 9, 2023 · 5 comments · Fixed by #4373
Closed

Scala 3 Ambiguous Implicits in UnorderedFoldable #4370

bpholt opened this issue Jan 9, 2023 · 5 comments · Fixed by #4373
Assignees
Labels
Milestone

Comments

@bpholt
Copy link
Member

bpholt commented Jan 9, 2023

Using cats 2.9.0 and Scala 3.2.1, this code results in a migration warning (or an error in Scastie)

import cats.syntax.all._

val list: List[(String, String)] = List.empty

list
  .nested
  .map(_.length)
  .value

I saw this locally:

Migration Warning:
Ambiguous implicits method catsTraverseForSeq in object UnorderedFoldable and method catsTraverseForList in object UnorderedFoldable seem to be used to implement a local failure in order to negate an implicit search. According to the new implicit resolution rules this is no longer possible; the search will fail with a global ambiguity error instead.

Consider using the scala.util.NotGiven class to implement similar functionality.

and see this error in Scastie (which is actually Scala 3.2.0, and isn't cross-building with sbt-typelevel, like I was locally)

Found: cats.data.Nested[F, G, A]
Required: ?{ map: ? }

where: A is a type variable with constraint >: String
F is a type variable with constraint >: List and <: [_] =>> Any
G is a type variable with constraint >: [T2] =>> (String, T2) and <: [_] =>> Any

Note that implicit conversions cannot be applied because they are ambiguous;
both method catsTraverseForList in object UnorderedFoldable and method catsTraverseForSeq in object UnorderedFoldable match type cats.Traverse[F]

@armanbilge
Copy link
Member

If anyone wants to give it a try: I wonder if we can fix this by de-prioritizing the Seq instances in implicit search:

implicit def catsTraverseForList: Traverse[List] = cats.instances.list.catsStdInstancesForList
implicit def catsTraverseForSeq: Traverse[Seq] = cats.instances.seq.catsStdInstancesForSeq
implicit def catsTraverseForVector: Traverse[Vector] = cats.instances.vector.catsStdInstancesForVector

@armanbilge armanbilge added this to the 2.10.0 milestone Jan 9, 2023
@armanbilge armanbilge added the bug label Jan 9, 2023
@UlisesTorrella
Copy link
Contributor

Hi, if no one is working on it yet I can give it a try

@armanbilge
Copy link
Member

@UlisesTorrella yes please, thank you!

@UlisesTorrella
Copy link
Contributor

UlisesTorrella commented Jan 14, 2023

I've implemented the de-prioritization of the seq instances in UnorderedFoldable like this:

trait LowPriorityImplicits {
  implicit def catsTraverseForSeq: Traverse[Seq] = cats.instances.seq.catsStdInstancesForSeq
}

object UnorderedFoldable
    extends ScalaVersionSpecificTraverseInstances
    with cats.instances.NTupleUnorderedFoldableInstances with LowPriorityImplicits{ ...

And though it solves this issue, it rises plenty warnings of the type the type test for cats.*.*[A] cannot be checked at runtime
I'll be checking other alternatives later, unless it's ok like this (I'm new to the repo)

@armanbilge
Copy link
Member

@UlisesTorrella nice work! Would you mind putting up a PR for further discussion, and to give CI a crack at it? Thanks!

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

Successfully merging a pull request may close this issue.

3 participants