I need to iterate a list of base trait items and based on certain condition group all instances of a specific type. But i am facing a syntax error on one of the branches of the match clause inside the foldLeft.
trait Base {
def foo: Int
}
case class TypeA(foo: Int) extends Base
case class TypeB(foo: Int) extends Base
object Main {
def main(args: Array[String]): Unit = {
val items: Seq[Base] = Seq(TypeA(1), TypeB(2), TypeB(3), TypeA(2), TypeA(3))
val typeAs: Seq[Seq[TypeA]] = items.foldLeft(Seq.empty[Seq[TypeA]]) { case (acc, item) =>
val someCondition = true
item match {
case el:TypeA =>
if (acc.nonEmpty) {
if (someCondition)
acc :+ Seq(Seq(el)) // type mismatch error here
else
acc.init :+ (acc.last :+ el)
} else
Seq(Seq(el))
case _:TypeB =>
acc
}
}
}
}
The error on compilation is
error: type mismatch;
found : Seq[Seq[Equals]]
required: Seq[Seq[TypeA]]
acc :+ Seq(Seq(el))
Using Scala 2.13.3 via the maven compiler plugin.
Ahh, made a careless mistake Should be trying to append a Seq[TypeA] to the accumulator
acc :+ Seq(el)