Skip to content

Commit

Permalink
fix show interpolation ambiguity with Seq subtypes (#3852)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Nielens <tim.nielens@restore.energy>
  • Loading branch information
tnielens and Tim Nielens authored Apr 6, 2021
1 parent 7cd2911 commit 9753b34
Show file tree
Hide file tree
Showing 2 changed files with 17 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]
}
12 changes: 12 additions & 0 deletions tests/src/test/scala/cats/tests/ShowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9753b34

Please sign in to comment.