Skip to content

Commit

Permalink
fix show interpolation ambiguity with Seq subtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Nielens committed Apr 6, 2021
1 parent 7cd2911 commit 52dbf90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions core/src/main/scala/cats/Show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ object Show extends ScalaVersionSpecificShowInstances with ShowInstances {
implicit def catsShowForOption[A: Show]: Show[Option[A]] = cats.instances.option.catsStdShowForOption[A]
implicit def catsShowForTry[A: Show]: Show[Try[A]] = cats.instances.try_.catsStdShowForTry[A]
implicit def catsShowForList[A: Show]: Show[List[A]] = cats.instances.list.catsStdShowForList[A]
implicit def catsShowForSeq[A: Show]: Show[Seq[A]] = cats.instances.seq.catsStdShowForSeq[A]
implicit def catsShowForVector[A: Show]: Show[Vector[A]] = cats.instances.vector.catsStdShowForVector[A]
implicit def catsShowForQueue[A: Show]: Show[Queue[A]] = cats.instances.queue.catsStdShowForQueue[A]
implicit def catsShowForEither[A: Show, B: Show]: Show[Either[A, B]] =
Expand All @@ -104,7 +103,11 @@ object Show extends ScalaVersionSpecificShowInstances with ShowInstances {
def catsShowForTuple2[A: Show, B: Show]: Show[(A, B)] = cats.instances.tuple.catsStdShowForTuple2[A, B]
}

private[cats] trait ShowInstances extends cats.instances.NTupleShowInstances {
private[cats] trait ShowInstances extends cats.instances.NTupleShowInstances with ShowInstances0 {
implicit def catsShowForFiniteDuration: Show[FiniteDuration] =
cats.instances.finiteDuration.catsStdShowForFiniteDurationUnambiguous
}

private[cats] trait ShowInstances0 {
implicit def catsShowForSeq[A: Show]: Show[Seq[A]] = cats.instances.seq.catsStdShowForSeq[A]
}
11 changes: 11 additions & 0 deletions tests/src/test/scala/cats/tests/ShowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ class ShowSuite extends CatsSuite {
show"instance resolution is not ambiguous for ${FiniteDuration(3L, TimeUnit.SECONDS)}"
show"instance resolution is not ambiguous for ${Duration(3L, TimeUnit.SECONDS)}"
}

test("show interpolation with Seq subtypes isn't ambiguous") {
implicitly[ContravariantShow[Seq[Int]]]
implicitly[ContravariantShow[List[Int]]]
implicitly[ContravariantShow[Vector[Int]]]

val goodmornings = Seq("guten Tag", "good morning", "bonjour")
assertEquals(show"$goodmornings", "List(guten Tag, good morning, bonjour)")
assertEquals(show"${goodmornings.toList}", "List(guten Tag, good morning, bonjour)")
assertEquals(show"${goodmornings.toVector}", "Vector(guten Tag, good morning, bonjour)")
}
}

final class ShowSuite2 extends munit.FunSuite {
Expand Down

0 comments on commit 52dbf90

Please sign in to comment.