我需要将任意嵌套Map转换为TreeMap。例子:
Map[Int, String] -> TreeMap[Int, String]
Map[Int, Map[Int, String]] -> TreeMap[Int, TreeMap[Int, String]]
... etc
我有类型体操和隐式转换的工作解决方案,但我有类型问题
trait ToTreeMap[M[_, _], K, V] {
type Result
def treeMap(x: M[K, V]): TreeMap[K, Result]
}
trait LowerPriorityToTreeMap {
implicit def plainMap[K, V](implicit ord: Ordering[K]): ToTreeMap[Map, K, V] =
new ToTreeMap[Map, K, V] {
type Result = V
def treeMap(x: Map[K, V]): TreeMap[K, Result] = TreeMap(x.toArray: _*)
}
}
object ToTreeMap extends LowerPriorityToTreeMap {
implicit def nestedMap[K1, K2, V](implicit inner: ToTreeMap[Map, K2, V], ord: Ordering[K1]): ToTreeMap[Map, K1, Map[K2, V]] = {
new ToTreeMap[Map, K1, Map[K2, V]] {
type Result = TreeMap[K2, inner.Result]
def treeMap(x: Map[K1, Map[K2, V]]): TreeMap[K1, Result] = TreeMap(x.mapValues(v => inner.treeMap(v)).toArray: _*)
}
}
implicit class MapOps[K, V](map: Map[K, V]) {
def asTreeMap(implicit as: ToTreeMap[Map, K, V]) = as.treeMap(map)
}
}
这样可以正常工作,但是类型ToTreeMap [..] #Result与我需要的TreeMap不匹配
import ToTreeMap._
val map: Map[Int, Map[Int, Map[Int, String]]] =
Map(
1000 -> Map(
100 -> Map(
10 -> "aa",
1 -> "bb"
),
99 -> Map(
10 -> "aa",
1 -> "bb"
)
),
999 -> Map(
100 -> Map(
10 -> "aa",
1 -> "bb"
),
99 -> Map(
10 -> "aa",
1 -> "bb"
)
)
)
val m = Map(1 -> "aaa")
println(map.asTreeMap)
println(m.asTreeMap)
// This line fails to compile
val tm: TreeMap[Int, TreeMap[Int, TreeMap[Int, String]]] = map.asTreeMap
任何想法如何正确地做到这一点?
Scalac在这个示例中选择了正确的类型:Two dimensional array as a function这与我认为没有太大不同
我可以用' asIntanceOf'来解决这类问题。但是如果没有这种肮脏的黑客,我想做到这一点。
答案 0 :(得分:4)
您需要确保不要丢弃实例方法的返回类型中的Return
值:
trait ToTreeMap[M[_, _], K, V] {
type Result
def treeMap(x: M[K, V]): TreeMap[K, Result]
}
trait LowerPriorityToTreeMap {
implicit def plainMap[K, V](
implicit ord: Ordering[K]
): ToTreeMap[Map, K, V] { type Result = V } =
new ToTreeMap[Map, K, V] {
type Result = V
def treeMap(x: Map[K, V]): TreeMap[K, Result] = TreeMap(x.toArray: _*)
}
}
object ToTreeMap extends LowerPriorityToTreeMap {
implicit def nestedMap[K1, K2, V](
implicit inner: ToTreeMap[Map, K2, V], ord: Ordering[K1]
): ToTreeMap[Map, K1, Map[K2, V]] {
type Result = TreeMap[K2, inner.Result]
} = {
new ToTreeMap[Map, K1, Map[K2, V]] {
type Result = TreeMap[K2, inner.Result]
def treeMap(x: Map[K1, Map[K2, V]]): TreeMap[K1, Result] =
TreeMap(x.mapValues(v => inner.treeMap(v)).toArray: _*)
}
}
implicit class MapOps[K, V](map: Map[K, V]) {
def asTreeMap(implicit as: ToTreeMap[Map, K, V]) = as.treeMap(map)
}
}
这很有效,对我来说看起来非常合理。