Scala - 使用toMap时发散隐式扩展

时间:2013-05-08 15:08:11

标签: scala map implicit-conversion

我有一个枚举的以下定义:

object GraphType extends Enumeration {
  type Type = Value
  val MESSAGE, REQUEST, ERRORS = Value
}

现在我尝试将每种类型映射到相应的新TimeSeries,如下所示:

val dataSets = ( GraphType.values map (graphType => graphType -> new TimeSeries(graphType)) ).toMap

类型系统将数据集列为Map[GraphType.Value, TimeSeries],这正是我想要的。但是,编译失败并显示错误消息:

error: diverging implicit expansion for type scala.collection.generic.CanBuildFrom[ird.replay.gui.GraphType.ValueSet,(ird.replay.gui.GraphType.Value, org.jfree.data.time.TimeSeries),That]
starting with method newCanBuildFrom in object SortedSet
val dataSets = GraphType.values map (graphType => graphType -> new TimeSeries(graphType)) toMap

COuld有人为此提供了一些解释,相当神秘,错误消息?感谢

1 个答案:

答案 0 :(得分:2)

首先尝试将枚举的值集转换为List,如下所示:

val dataSets = (GraphType.values.toList.map(gt => (gt, new TimeSeries(gt)))).toMap

有关作为套装的内容与您尝试将其转换为Map的方式不一致,但它似乎与List

一样正常