Show

The Show type class is an abstraction that provides the ability to convert values of any type into their string representation. It is an alternative to the toString method that allows flexible customization of output formatting for various data types.

The Show type class defines one primary operation:

Where is it used?

Advantages over toString:

Formal definition

Show is a type class that provides an abstraction for converting objects into their string representation.

For a correct implementation of Show, the following laws must hold:

Code definition in Scala

trait Show[A]:
  def show(a: A): String

Implementation in libraries

Implementation in Cats

import cats.*
import cats.implicits.*
123.show     // 123
"abc".show   // abc

Implementation in ScalaZ

import scalaz.*
import Scalaz.*
1.0.shows // "1.0"

References: