All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hammerlab.io.Print.scala Maven / Gradle / Ivy

There is a newer version: 5.2.1
Show newest version
package org.hammerlab.io

import java.io.{ ByteArrayOutputStream, PrintStream }

/**
 * Wrapper for implementing [[Show]]s using a [[Printer]].
 *
 * Example syntax (from test):
 *
 * implicit val showA =
 *     Print[A] {
 *       new Print(_) {
 *         val A(n, s) = t
 *         echo(s"$n, $s")
 *       }
 *     }
 *
 * [[CanPrint.echo echo]] and other [[CanPrint]] utilities are availabe to write to an implicit [[Printer]] wrapping a
 * [[ByteArrayOutputStream]] from which a string-representation is parsed.
 */
abstract class Print[T](val t: T)
  extends CanPrint
    with Serializable {
  val bytes = new ByteArrayOutputStream()
  implicit val stringPrinter = Printer(new PrintStream(bytes))

  lazy val shown = bytes.toString()
}

object Print {
  def apply[T](fn: T ⇒ Print[T]): cats.Show[T] = cats.Show.show[T](fn(_).shown)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy