I am trying to define an instance of Show
(from cats 0.9) that can be used for all members of an ADT as follows:
import $ivy.`org.typelevel::cats:0.9.0`, cats.Show
sealed abstract class Colour(val name: String)
implicit val ColourShow = new Show[Colour] {
def show(c: Colour) = c.name
}
object Colour {
object Red extends Colour("Red")
object Blue extends Colour("Blue")
}
import Show._
println(Colour.Red.show)
An applicable instance cannot be found for Red
, however:
Compiling /Users/Rich/Projects/worksheets/fp-patterns/Colours.sc
/Users/Rich/Projects/worksheets/fp-patterns/Colours.sc:16: value show is not a member of object ammonite.$file.Colours.Colour.Red
val res_5 = println(Colour.Red.show)
^
Compilation Failed
Is it possible to use typeclasses in this way? I am trying to avoid having to define a separate instance for each concrete instanct of Colour.