title: How to avoid using asInstanceOf in Scala
body: <p>Currently my code needs class casting</p>
  
  <pre><code>val dataWriter: BytesDataWriter = createDataWriter
  
   def createDataWriter(p: SomeClass) =
      p.create_datawriter().asInstanceOf[BytesDataWriter]
  </code></pre>
  
  <p>The <code>create_datawriter</code> method will return the superclass DataWriter. Instead of casting it using <code>asInstanceOf</code>, I tried this approach </p>
  
  <pre><code>val dataWriter: BytesDataWriter = createDataWriter(p)  match {
        case writer: BytesDataWriter =&gt; writer
        case _ =&gt; throw new ClassCastException
      }
  </code></pre>
  
  <p>This is too verbose, and in case case does not work. Is there a better alternative to class casting?</p>