title: Scala: Convert multiple functions to single generic function?
body: <p>I have the following family of functions (two of which are shown) that I would like to implement as a single generic function:</p>
  
  <pre class="lang-scala prettyprint-override"><code>case class RichSeqInt(seq: Seq[Int]) extends AnyVal {
    def toByteArray: Array[Byte] = {
      val buffer = ByteBuffer.allocate(4 * seq.length)
      buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN)
      seq.foreach(buffer.putInt)
      buffer.array()
    }
  }
  
  case class RichSeqLong(seq: Seq[Long]) extends AnyVal {
    def toByteArray: Array[Byte] = {
      val buffer = ByteBuffer.allocate(8 * seq.length)
      buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN)
      seq.foreach(buffer.putLong)
      buffer.array()
    }
  }
  </code></pre>
  
  <p>I can't figure out how to generically get the primitive object's size and then how to generically put it into the buffer even though this seems like something that should be doable.</p>
  
  <p>Is this possible?</p>