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

fix show interpolation ambiguity with Seq/List #3852

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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