In Haskell I can define a generic type for a tree as:
type Tree t = Leaf t | Node (Tree t) (Tree t)
If I want to define a function for a specific parameterization of Tree, I can simply do:
-- Signature of a function that takes a tree of bool
foo :: Tree Bool -> Int
-- Takes a tree of numbers
bar :: (Num n) => Tree n -> Bool
We can define a similar tree type in Scala with:
abstract class Tree[T]()
case class Leaf[T](t: T) extends Tree[T]
case class Node[T](left: Tree[T], right: Tree[T]) extends Tree[T]
But how can I define a method for Tree that only applies to certain types? Do I need to use inheritance or there's a way to say:
abstract class Tree[T]() {
// Method only for Tree[String]:
def foo[String] = ...
}