As far as i understand, @specialized
annotation should generate some unboxed code for every primitive type i mentioned, but this doesn't work:
scala> def aaa[@specialized(Int, Double, Float, Long) T] = (5.0).doubleValue.asInstanceOf[T]
aaa: [T]=> T
scala> aaa[Int]
unrecoverable error (inside interpreter/compiler)
This compiles:
scala> def aaa[@specialized(Int, Double, Float, Long) T](a: T) = (5.0).doubleValue.asInstanceOf[T]
aaa: [T]=> T
scala> aaa[Int](0)
ClassCastException
But it still uses boxed type for asInstanceOf[T]. This obviously works:
scala> (5.0).asInstanceOf[Int]
res28: Int = 5
UPDATE:
Type erasure and answers like that Writing a generic cast function Scala has nothing to do with my problem. Type erasure just preventing compiler from adding typecast
byte-code operation for generics, but eventually it will be added - see ClassCastException
(generated by this op) in my REPL