Skip to content

Commit

Permalink
Make Show contravariant
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundnoble committed May 5, 2017
1 parent 4e381d2 commit 20add21
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/Show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import cats.functor.Contravariant
* made a toString method, a Show instance will only exist if someone
* explicitly provided one.
*/
@typeclass trait Show[T] {
def show(f: T): String
@typeclass trait Show[-T] {
def show(t: T): String
}

object Show {
Expand Down
7 changes: 1 addition & 6 deletions core/src/main/scala/cats/instances/bitSet.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package cats.instances

import scala.collection.immutable.BitSet
import cats.Show

trait BitSetInstances extends cats.kernel.instances.BitSetInstances {
implicit def catsStdShowForBitSet: Show[BitSet] = Show.fromToString[BitSet]
}
trait BitSetInstances extends cats.kernel.instances.BitSetInstances
21 changes: 0 additions & 21 deletions tests/src/test/scala/cats/tests/BitSetTests.scala

This file was deleted.

16 changes: 15 additions & 1 deletion tests/src/test/scala/cats/tests/ShowTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class ShowTests extends CatsSuite {
checkAll("Contravariant[Show]", SerializableTests.serializable(Contravariant[Show]))

test("show string interpolator") {
import cats.syntax.show._

case class Cat(name: String)
object Cat {
Expand All @@ -31,4 +30,19 @@ class ShowTests extends CatsSuite {

assertResult("Good morning, Whiskers!")(show"Good $tod, ${List(cat).head}!")
}

test("show string interpolator and contravariance") {
trait Animal {
val name: String
}
object Animal {
implicit val showAnimal: Show[Animal] =
Show.show(_.name)
}
class Cat(override val name: String, val breed: String) extends Animal

val cat = new Cat("Whiskers", "Calico")

assertResult("Good morning, Whiskers!")(show"Good morning, $cat!")
}
}

0 comments on commit 20add21

Please sign in to comment.