From f5b99e6ff34d0a4361365388c094576444df7f51 Mon Sep 17 00:00:00 2001 From: Igor Ramazanov Date: Fri, 15 Aug 2025 07:50:21 +0000 Subject: [PATCH] Add `Show.by` --- core/src/main/scala/cats/Show.scala | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/src/main/scala/cats/Show.scala b/core/src/main/scala/cats/Show.scala index dba5f4f4e0..e903274b1c 100644 --- a/core/src/main/scala/cats/Show.scala +++ b/core/src/main/scala/cats/Show.scala @@ -25,6 +25,7 @@ import java.util.UUID import scala.collection.immutable.{BitSet, Queue, Seq, SortedMap, SortedSet} import scala.concurrent.duration.{Duration, FiniteDuration} import scala.util.Try +import scala.{specialized => sp} /** * A type class to provide textual representation. It is meant to be a @@ -65,6 +66,13 @@ object Show extends ScalaVersionSpecificShowInstances with ShowInstances { */ def show[A](f: A => String): Show[A] = f(_) + /** + * Convert an implicit `Show[B]` to a `Show[A]` using the given + * function `f`. + */ + def by[@sp A, @sp B](f: A => B)(implicit ev: Show[B]): Show[A] = + a => ev.show(f(a)) + /** * creates an instance of [[Show]] using object toString */