title: When can Scala's implicitly return null?
body: <p>I was recently surprised when invocations of Scala's <code>implicitly</code> was returning null during runtime.  I had thought that was unlikely, given that the code shouldn't compile if no implicit instance was available in implicit scope.  When is implicitly allowed to return null?  Is this a compiler limitation, or is this expected behavior?</p>
  
  <p>Here's some context, if it helps.  I'm using shapeless to derive typeclass instances for persisting arbitrary nested case classes.  I thought it would be helpful to use implicitly in the nested case classes to check if a typeclass instance could be derived, as it can be unclear where to start looking if the nested case class is large.</p>
  
  <p>So, for example, if I was trying to persist:</p>
  
  <pre><code>case class Foo(bar: Bar, baz: Baz)
  </code></pre>
  
  <p>and the compiler couldn't derive an instance for my formatter <code>MyFormatter[Foo]</code>, I started doing something like the following:</p>
  
  <pre><code>case class Bar(i: Int, q: Qux)
  object Bar {
    implicit val formatter = implicitly[MyFormatter[Bar]]
  }
  </code></pre>
  
  <p>expecting the compiler to tell me that it couldn't find an implicit instance of <code>MyFormatter[Bar]</code>.</p>
  
  <p>Instead, this was a terrible idea, and my code compiled (when it shouldn't have, as no typeclass instance for Qux could be derived) and <code>Bar.formatter</code> was null at runtime.</p>