Skip to content

Commit

Permalink
Render typeclass; 9.5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Mar 5, 2024
1 parent 2328bdf commit 51724ba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
lazy val scalalib = Project("scalalib", file("."))
organization := "com.github.ornicar"
name := "scalalib"
version := "9.5.7"
version := "9.5.8"
scalaVersion := "3.3.3"
licenses += "MIT" -> url("https://opensource.org/licenses/MIT")
libraryDependencies += "org.typelevel" %% "cats-core" % "2.10.0"
Expand Down
18 changes: 18 additions & 0 deletions src/main/scala/Render.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ornicar.scalalib

/* Like cats.Show,
* except it's actually fine to render it to an end-user.
* cats.Show has a default instance for Option[A] that renders
* as `None` or `Some(value)`, which is not very user-friendly.
*/
@FunctionalInterface
trait Render[A]:
def apply(a: A): String
extension (a: A) def render: String = apply(a)

object Render:
def apply[A](f: A => String): Render[A] = f(_)

/* If we have Render, we have Show;
* but not the other way around. */
given [A](using Render[A]): cats.Show[A] = cats.Show.show(_.render)
3 changes: 2 additions & 1 deletion src/main/scala/newtypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ object newtypes:
extension (a: Newtype) inline def apply: Impl = a

trait OpaqueString[A](using A =:= String) extends TotalWrapper[A, String]:
given Show[A] = Show.show(_.value)
given Show[A] = _.value
given Render[A] = _.value

trait OpaqueInt[A](using A =:= Int) extends TotalWrapper[A, Int]:
extension (inline a: A)
Expand Down

0 comments on commit 51724ba

Please sign in to comment.