I have the following code:
val obj: Int = 5;
var objType: Class[_] = obj.getClass
objType match {
case _: Int =>
byeBuffer.putInt(asInstanceOf[Int])
case _: Long =>
byeBuffer.putLong(asInstanceOf[Long])
case _: Float =>
byeBuffer.putFloat(asInstanceOf[Float])
case _: Double =>
byeBuffer.putDouble(asInstanceOf[Double])
case _: Boolean => {
val byte = if (asInstanceOf[Boolean]) 1 else 0
byeBuffer.put(byte.asInstanceOf[Byte])
}
case default => throw new UnsupportedOperationException("Type not supported: " + default.getClass)
}
This code is broken. However if I used obj instead of objType then it would work. But I want to use specifically objType in the pattern matching.