![JAR search and dependency download from the Maven repository](/logo.png)
cats.Show.scala Maven / Gradle / Ivy
package cats
import simulacrum.typeclass
import cats.functor.Contravariant
/**
* A type class to provide textual representation. It is meant to be a
* better "toString". Whereas toString exists for any Object,
* regardless of whether or not the creator of the class explicitly
* made a toString method, a Show instance will only exist if someone
* explicitly provided one.
*/
@typeclass trait Show[T] extends Serializable {
def show(f: T): String
}
object Show {
/** creates an instance of [[Show]] using the provided function */
def show[A](f: A => String): Show[A] = new Show[A] {
def show(a: A): String = f(a)
}
/** creates an instance of [[Show]] using object toString */
def fromToString[A]: Show[A] = new Show[A] {
def show(a: A): String = a.toString
}
implicit val showContravariant: Contravariant[Show] = new Contravariant[Show] {
def contramap[A, B](fa: Show[A])(f: B => A): Show[B] =
show[B](fa.show _ compose f)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy