title: Defining implicit view-bounds on Scala traits
body: <p>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 something like this:</p>
  
  <pre><code>trait TreeNode[A] {
      def isLeaf: Boolean
      def traverse: Seq[A]
      ...
  }
  
  case class Branch[A](value: A, left: TreeNode[A], right: TreeNode[A]) extends TreeNode[A] { 
     def isLeaf: Boolean = false
     def traverse: Seq[A] = ...
     ... 
  }
  
  case class Leaf[A]() extends TreeNode[A] { 
      def isLeaf: Boolean = true
      def traverse: Seq[A] = Seq[A]()
      ... 
  }
  </code></pre>
  
  <p>I'd <strong><em>like</em></strong> to put a type constraint on <code>A</code> so that it will only accept objects that extend <code>Ordered</code>. It looks like I need to define a view bound on A <em>(<code>[A &lt;% Ordered[A]]</code>)</em> on <code>Branch</code> and <code>Leaf</code>, as well as the <code>TreeNode</code> trait.. I can't do this on the <code>TreeNode</code> trait, however, because view bounds aren't accepted.</p>
  
  <p>As I understand, <code>&lt;%</code>-style view-bounds are syntactic sugar for an <code>implicit</code> definition, so there should be a way to write to define the bound manually within the <code>TreeNode</code> trait. I'm not sure how I'm supposed to do this, though. I've looked around a bit, but haven't gotten much further than that some sort of implicit needs to be defined.</p>
  
  <p>Can anybody point me in the right direction? Am I approaching this from the wrong angle entirely?</p>