Render
Render
type-class
It is to provider render
functionality to render a given type A
into String
.
trait Render[A] {
def render(a: A): String
}
Usage Example
import extras.render.Render
final case class Foo(id: Int, name: String)
object Foo {
implicit val fooRender: Render[Foo] = new Render[Foo] {
def render(foo: Foo): String =
s"ID=${foo.id.toString} / Name=${foo.name}"
}
// OR just
// implicit val fooRender: Render[Foo] =
// foo => s"ID=${foo.id.toString} / Name=${foo.name}"
}
def bar[A: Render](a: A): Unit =
println(s">> a: ${Render[A].render(a)}")
bar(Foo(1, "Something"))
// >> a: ID=1 / Name=Something
Render.render
constructor method
Render.render[A](A => String): Render[A]
Usage Example
import extras.render.Render
final case class Foo(id: Int, name: String)
object Foo {
implicit val fooRender: Render[Foo] = Render.render(
foo => s"ID=${foo.id.toString} / Name=${foo.name}"
)
}
def bar[A: Render](a: A): Unit =
println(s">> a: ${Render[A].render(a)}")
bar(Foo(1, "Something"))
// >> a: ID=1 / Name=Something
Render
from Existing Render
If the value of your type can be rendered by an existing Render
, you can easily create Render
for your type from the existing one with contramap
.
Render[A].contramap[B](A => B): Render[B]
Usage Example
import java.util.UUID
import extras.render.Render
final case class Id(value: UUID) extends AnyVal
object Id {
implicit val idRender: Render[Id] = Render[UUID].contramap(_.value)
}
import extras.render.syntax._
Id(UUID.randomUUID()).render
// res5: String = "c442b977-f242-43bd-a84d-7200948c2ca1"
final case class Name(value: String) extends AnyVal
object Name {
implicit val nameRender: Render[Name] = Render[String].contramap(_.value)
}
Name("Kevin").render
// res6: String = "Kevin"