I\'m doing an exercise to implement a functional binary-search-tree in Scala, following a similar pattern that I\'ve seen used in Haskell. I have a structure that looks somethin
You could provide the "evidence" that A
is Ordered
by requiring an abstract member of type Ordered[A]
on the trait
:
trait TreeNode[A] {
implicit val evidence: Ordered[A]
}
You would then be forced to provide this in any concrete subtypes, this proving that A
is Ordered
:
case class Leaf[A](value: A)(implicit ev: Ordered[A]) extends TreeNode[A] {
val evidence = ev
}
You might instead want to constrain A
to a type which has an implicit Ordering[A]
- this is not an inheritance relationship; it is more like a haskell typeclass. But the implementation in terms of the above technique would be the same.