In this post we’ll look at Scalaz Show. The only purpose of this trait is to provide a String representation for its subtypes.
A Simple Example
1 | @ import scalaz._ |
As always, there are implicits defined for Int, Float, etc. Calling println returns a String whereas calling show returns a Cord. Going over the source code for Cord:
A Cord is a purely functional data structure for efficiently storing and manipulating Strings that are potentially very long.
Why Use Show?
A reasonable question to ask is why use Show when we have a toString which produces a String representation of objects. The answer is that toSring doesn’t always produce a useful representation.
1 | @ println(new Thread()) |
Showing a Thread
So, let’s create a Show for Thread.
1 | @ implicit object ThreadShowable extends Show[Thread] { |
Conclusion
Show is probably not that interesting and probably exists because there is Show class in Haskell[1]:
The instances of class Show are those types that can be converted to character strings (typically for I/O)