In the following function, I get a compiler warning that I don’t 100% understand.
def treeReduce[A, B](objects: TraversableOnce[A], init: B, g: A => B, f: (B, B) => B): B = {
def consumeStack(stack: List[(Int, B)]): List[(Int, B)] = {
stack match {
case (i, a1) :: (j, a2) :: tail if i == j => consumeStack((i + 1, f(a1, a2)) :: tail)
case _ => stack
}
}
val stack = objects.foldLeft((1, init) :: Nil) { case (stack: List[(Int, B)], ob: A) =>
(1, g(ob)) :: consumeStack(stack)
}
// there may be up to log_2 of objs.size many pairs left on the stack
assert(stack != Nil)
stack.tail.map(_._2).foldLeft(stack.head._2)(f)
}
The warning is:
Warning:(419, 84) abstract type pattern A is unchecked since it is eliminated by erasure
val stack = objs.foldLeft((1, init) :: Nil) { case (stack: List[(Int, B)], ob: A) =>
I don’t understand what it is that is not being checked? Is it saying it doesn’t know whether ob
is of type A
. In my mind ob
is guaranteed to be of type A
because objects
is Traversable[A]
, so foldLeft
promises that this second argument has type A
.
Or is the compiler telling me that it does not know whether objects
is really of type Traversable[A]
?
Or is the problem elsewhere?