title: Implicit conversion vs. type class
body: <p>In Scala, we can use at least two methods to retrofit existing or new types. Suppose we want to express that something can be quantified using an <code>Int</code>. We can define the following trait.</p>
  
  <h3>Implicit conversion</h3>
  
  <pre><code>trait Quantifiable{ def quantify: Int }
  </code></pre>
  
  <p>And then we can use implicit conversions to quantify e.g. Strings and Lists.</p>
  
  <pre><code>implicit def string2quant(s: String) = new Quantifiable{ 
    def quantify = s.size 
  }
  implicit def list2quantifiable[A](l: List[A]) = new Quantifiable{ 
    val quantify = l.size 
  }
  </code></pre>
  
  <p>After importing these, we can call the method <code>quantify</code> on strings and lists. Note that the quantifiable list stores its length, so it avoids the expensive traversal of the list on subsequent calls to <code>quantify</code>.</p>
  
  <h3>Type classes</h3>
  
  <p>An alternative is to define a "witness" <code>Quantified[A]</code> that states, that some type <code>A</code> can be quantified.</p>
  
  <pre><code>trait Quantified[A] { def quantify(a: A): Int }
  </code></pre>
  
  <p>We then provide instances of this type class for <code>String</code> and <code>List</code> somewhere.</p>
  
  <pre><code>implicit val stringQuantifiable = new Quantified[String] {
    def quantify(s: String) = s.size 
  }
  </code></pre>
  
  <p>And if we then write a method that needs to quantify its arguments, we write:</p>
  
  <pre><code>def sumQuantities[A](as: List[A])(implicit ev: Quantified[A]) = 
    as.map(ev.quantify).sum
  </code></pre>
  
  <p>Or using the context bound syntax:</p>
  
  <pre><code>def sumQuantities[A: Quantified](as: List[A]) = 
    as.map(implicitly[Quantified[A]].quantify).sum
  </code></pre>
  
  <h3>But when to use which method?</h3>
  
  <p>Now comes the question. How can I decide between those two concepts?</p>
  
  <p>What I have noticed so far.</p>
  
  <p><strong>type classes</strong></p>
  
  <ul>
  <li>type classes allow the nice context bound syntax</li>
  <li>with type classes I don't create a new wrapper object on each use</li>
  <li>the context bound syntax does not work anymore if the type class has multiple type parameters; imagine I want to quantify things not only with integers but with values of some general type <code>T</code>. I would want to create a type class <code>Quantified[A,T]</code></li>
  </ul>
  
  <p><strong>implicit conversion</strong></p>
  
  <ul>
  <li>since I create a new object, I can cache values there or compute a better representation; but should I avoid this, since it might happen several times and an explicit conversion would probably be invoked only once?</li>
  </ul>
  
  <h3>What I expect from an answer</h3>
  
  <p>Present one (or more) use case(s) where the difference between both concepts matters and explain why I would prefer one over the other. Also explaining the essence of the two concepts and their relation to each other would be nice, even without example.</p>