我有以下变量series
:
var series: List[FlotSerie] = List(
new FlotSerie() {
override val label = Full("Min")
},
new FlotSerie() {
override val label = Full("Max")
},
new FlotSerie() {
override val label = Full("Avg")
}
)
不幸的是,我使用以下方法遇到编译器错误,该方法采用新数据点并根据新数据和旧系列更新series
新数据点。List[FlotSeries]
def updateSeries(sample: Sample): List[FlotSerie] = {
series = series.map(serie =>
serie match {
case item if item.label == Full("Min") => {
new FlotSerie() {
override val label = item.label
override val data = (sample.timestamp.toDouble, sample.min) :: serie.data
}
}
case item if item.label == Full("Max") => {
new FlotSerie() {
override val label = item.label
override val data = (sample.timestamp.toDouble, sample.max) :: serie.data
}
}
case item if item.label == Full("Avg") => {
new FlotSerie() {
override val label = item.label
override val data = (sample.timestamp.toDouble, sample.avg) :: serie.data
}
}
}
)
}
Scala 编译器在重新分配时窒息,因为它发现类型不匹配:
error: type mismatch;
found : Unit
required: List[net.liftweb.widgets.flot.FlotSerie]
series = series.map(serie => serie match {
我在这里做错了什么?似乎它应该返回一个可以分配给的 List[FlotSeries] series
。由于编译器发现Unit
我想到了foreach
总是返回Unit
,我是但match
运算符返回匹配表达式的最后一个值,而不是Unit
。