The following code traverses some list of nodes and creates Paragraph or Heading objects for some of the nodes:
abstract class Element
case class Paragraph(node: Node) extends Element
case class Heading(mainNode: Node, textNode: Node) extends Element
val elements =
parent.child.map(n =>
if (n.label == "p") Paragraph(n)
else if (n.label.matches("h\d")) Heading(n, n.child.head)
else None)
Next, I would like to get rid of the None elements and pass elements to some function that requires Seq[Element]. However, the type of elements is Seq[Product with Serializable] rather than Seq[Element]. Why, and how can I make the type stronger?
Use
collectinstead ofmapto keep only the elements you want:Anything that isn't defined within the
PartialFunctionyou pass tocollectis discarded.There's no reason to map to
Noneif you're just going to discard the elements anyway. And if you do for some reason want to keep theNoneinstances, then the others should be mapped toSome[Element]so that you'd haveSeq[Option[Element].