I think I might have found a compiler bug. I'm running 2.11.7.I have the following code which splits an array into an array of arrays before any element that satisfies the predicate `p`. It type checks:def splitBefore[T](a: Array[T], p: (T) => Boolean)(implicit tct: ClassTag[T]): Array[Array[T]] =a.foldLeft(Array[Array[T]](Array.empty[T])) {(acc: Array[Array[T]], s: T) => if (p(s))acc :+ Array(s)elseacc.init :+ (acc.last :+ s)}It works fine when I call it with non-empty `a`:scala> splitBefore(Array("a", "BC", "d"), (s: String) => s.size > 1)res1: Array[Array[String]] = Array(Array(a), Array(BC, d))But when I call it with an empty array, I get a `ClassCastException`:scala> splitBefore(Array.empty[String], (s: String) => s.size > 1)java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.String;... 33 elided
On Sat, Sep 26, 2015 at 11:59 AM, Roman Janusz <romeqj...@gmail.com> wrote:
It should use the class tag to create the array through reflection.
Definitely a bug. The cleanup phase is stripping away the outer array:
[[syntax trees at end of constructors]] // arr.scala
package <empty> {
object Test extends Object {
def arr(implicit evidence$1: scala.reflect.ClassTag): Array[Object] =
scala.Array.apply(scala.this.Predef.wrapRefArray(Array[Object]{null}), (ClassTag.apply(ScalaRunTime.this.arrayClass(evidence$1.runtimeClass())): scala.reflect.ClassTag)).$asInstanceOf[Array[Object]]();
def <init>(): Test.type = {
Test.super.<init>();
()
}
}
}
[[syntax trees at end of flatten]] // arr.scala: tree is unchanged since constructors
[[syntax trees at end of mixin]] // arr.scala: tree is unchanged since constructors
[[syntax trees at end of cleanup]] // arr.scala
package <empty> {
object Test extends Object {
def arr(implicit evidence$1: scala.reflect.ClassTag): Array[Object] = Array[Object]{null}.$asInstanceOf[Array[Object]]();
def <init>(): Test.type = {
Test.super.<init>();
()
}
}
}
Can you file it on the tracker?
HTH,
Vlad