miniboxing-plugin icon indicating copy to clipboard operation
miniboxing-plugin copied to clipboard

(new Array[AnyRef](5)).asInstanceOf[Array[T]] warning

Open VladUreche opened this issue 9 years ago • 1 comments

The dirtiest one of all:

$ mb-scala
Welcome to Scala version 2.11.6-20150224-172222-092690e7bf (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_80).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def foo[@miniboxed T] = new Array[AnyRef](5).asInstanceOf[Array[T]]
foo: [T]=> Array[T]

Warn, and the second time it happens, run sudo rm -rf / on the guys machine!

VladUreche avatar Jun 05 '15 22:06 VladUreche

To explain why it's the dirtiest one of all:

$ mb-scala
Welcome to Scala version 2.11.6-20150224-172222-092690e7bf (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_80).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def foo[@miniboxed T] = new Array[AnyRef](5).asInstanceOf[Array[T]]
foo: [T]=> Array[T]

scala> foo[Int](0) = 5
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [I
  ... 33 elided

VladUreche avatar Jun 05 '15 22:06 VladUreche