This is my second try to define the problem, I can't get my head around it.
I want to be able to define an algebraic type and define a simple typeclass over it, let's say Show
. In haskell I do:
data Tree a = EmptyTree | Node a deriving (Show)
Now, if I type EmptyTree
- haskell can show it, so it belongs to Show
.
Now I am trying to do the same in scala:
sealed abstract class Tree[+T]
case object EmptyTree extends Tree[Nothing]
case class Node[T](value: T) extends Tree[T]
Then I define Show
around it:
implicit def show[T] = Show.showA[Tree[T]]
I can do println((EmptyTree : Tree[Int]).show)
. But I can't do println(EmptyTree.show)
(response is value show is not a member of object EmptyTree
)
I have to write additional:
implicit class MyShowOps[A, +T <: Tree[A]](t: T) {
def showMy(implicit ev: Show[Tree[A]]): String = ev.shows(t)
}
And only then I can do println(EmptyTree.showMy)
It still doesn't sound correct, I believe either I am trying to do a wrong thing and I am not supposed to apply Show
like that and should use my construction only as Tree[T]
or I am missing a proper construction from Scalaz.