我有一个代码,它用产品解析来自 xml 的一些产品。有一个部分匹配,并将结果放入产品val:
val products =
for (event <- reader) {
event match {
case EvElemStart(_, "product", meta, _) =>
product = new Product()
product.title = meta("title").toString
product.description = meta("description").toString
case EvElemEnd(_, "product") =>
yield product // <--
case _ =>
}
}
它期望在yield之后出现“case” 。为什么?
以及如何以scala方式完成?
另一个尝试:
val products =
for (event <- reader) {
var title = "";
var description = "";
event match {
case EvElemStart(_, "product", meta, _) =>
title = meta("title").toString
description = meta("title").toString
case EvElemEnd(_, "product") =>
case _ =>
}
} yield new Product("", ""); // <---
编译器说它在yield之前需要“}”