Show

beginner

The Show typeclass abstracts the ability to obtain a String representation of any object.

It can be considered the typeclass equivalent of Java’s Object#toString.

import arrow.*
import arrow.instances.*

ForInt extensions { 1.show() }
// 1

Main Combinators

F#show

Given an instance of F it returns the String representation of this instance.

fun F.show(): String

Laws

Arrow provides ShowLaws in the form of test cases for internal verification of lawful instances and third party apps creating their own Show instances.

Creating your own Show instances

Show provides one special instance that can be potentially applicable to most datatypes. It uses kotlin’s toString method to get an object’s literal representation. This will work well in many cases, specially for data classes.

import arrow.core.*
import arrow.typeclasses.*

// Option is a data class with a single value
Show.any().run { Option.just(1).show() }
// Some(1)
// using invoke constructor
class Person(val firstName: String, val lastName: String)
val personShow = Show<Person> { "Hello $firstName $lastName" }

See Deriving and creating custom typeclass to provide your own Show instances for custom datatypes.

Data types

Module Data types
arrow.core Either, Id, Option, Try, Tuple10, Tuple2, Tuple3, Tuple4, Tuple5, Tuple6, Tuple7, Tuple8, Tuple9
arrow.data Ior, ListK, MapK, NonEmptyList, SequenceK, SetK, Validated
arrow.typeclasses Const

Type Class Hierarchy