From 9753b344df3c75e71799ea666d2018ee5f19747f Mon Sep 17 00:00:00 2001 From: Tim Nielens <11885535+tnielens@users.noreply.github.com> Date: Tue, 6 Apr 2021 23:53:04 +0200 Subject: [PATCH] fix show interpolation ambiguity with Seq subtypes (#3852) Co-authored-by: Tim Nielens --- core/src/main/scala/cats/Show.scala | 7 +++++-- tests/src/test/scala/cats/tests/ShowSuite.scala | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/Show.scala b/core/src/main/scala/cats/Show.scala index 9b1e03c6f6..de7b208a71 100644 --- a/core/src/main/scala/cats/Show.scala +++ b/core/src/main/scala/cats/Show.scala @@ -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]] = @@ -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] +} diff --git a/tests/src/test/scala/cats/tests/ShowSuite.scala b/tests/src/test/scala/cats/tests/ShowSuite.scala index ff56379880..a501a1a8ad 100644 --- a/tests/src/test/scala/cats/tests/ShowSuite.scala +++ b/tests/src/test/scala/cats/tests/ShowSuite.scala @@ -8,6 +8,7 @@ import cats.laws.discipline.{ContravariantTests, MiniInt, SerializableTests} import cats.laws.discipline.arbitrary._ import cats.laws.discipline.eq._ import java.util.concurrent.TimeUnit +import scala.collection.immutable.Seq import scala.concurrent.duration.{Duration, FiniteDuration} class ShowSuite extends CatsSuite { @@ -48,6 +49,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 {