title: Convert type pattern maching to type class
body: <p>I have the following code:</p>
  
  <pre><code>val byteBuffer = array(0) match {
    case _: Int =&gt;
      ByteBuffer.allocate(4 * array.length)
    case _: Long =&gt;
      ByteBuffer.allocate(8 * array.length)
    case _: Float =&gt;
      ByteBuffer.allocate(4 * array.length)
    case _: Double =&gt;
      ByteBuffer.allocate(8 * array.length)
    case _: Boolean =&gt;
      ByteBuffer.allocate(1 * array.length)
  }
  </code></pre>
  
  <p>How can I convert it to use type class?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I was asked what the type of the array is. It's complicated. The array is declared like that:</p>
  
  <pre><code>val array = obj.asInstanceOf[mutable.WrappedArray[Any]].array
  </code></pre>
  
  <p>obj is a parameter that the function accepts:</p>
  
  <pre><code>val createBuffer = (obj: Any, dType: DataType) =&gt; dType match {
  </code></pre>
  
  <p>The function is called here:</p>
  
  <pre><code>val byteBuffer: Option[ByteBuffer] = createBuffer(row.get(i), types(i))
  </code></pre>
  
  <p>row is a Spark DataFrame row.</p>