找不到参数排序的隐式值
尝试编译此错误时出现以下错误:
Btree.scala:9: 错误: 无法找到参数排序的隐式值: Ordering[K] 抽象类 Node[K,V] 扩展 TreeMap[K,V]
TreeMap 应该接受我提供的隐式 Ordering[A] val。也许隐式参数需要位于实例化 Btree(TreeMap) 的对象 Tester 中? 我更愿意将隐式声明保留在 Btree 类中,因为我希望 Ordering 有一个实现 Comparable[K] 的类型 K。有道理吗?
package disttree {
import scala.collection.immutable.TreeMap
class Btree[K <: Comparable[K],V](fanout:Int) {
implicit object DefaultOrder extends Ordering[K] {
def compare(k1: K, k2: K) = k1 compareTo k2
}
abstract class Node[K,V] extends TreeMap[K,V]
class InnerNode[K,V] extends Node[K,V]
class LeafNode[K,V] extends Node[K,V]
val root = new InnerNode[K,V]()
def search(n: Node[K,V], key: K): Option[(K,V)] = {
return n.find(_ == key)
}
def insert(key: K, value: V) { }
def delete(key: K) { }
}
}
import disttree._;
object Tester {
def main(args: List[String]) = {
var t = new Btree[Int, Any](2)
t.insert(1, "goodbye")
Console.println(t)
}
}
I get the following error when trying to compile this:
Btree.scala:9: error: could not find implicit value for parameter ordering: Ordering[K]
abstract class Node[K,V] extends TreeMap[K,V]
TreeMap is supposed to accept an implicit Ordering[A] val which I provide. Perhaps the implicit parameter needs to be in object Tester where the Btree(TreeMap) is instantiated?
I would prefer to keep the implicit declaration inside of the Btree class because I would like Ordering to have a type K that implements Comparable[K]. Make sense?
package disttree {
import scala.collection.immutable.TreeMap
class Btree[K <: Comparable[K],V](fanout:Int) {
implicit object DefaultOrder extends Ordering[K] {
def compare(k1: K, k2: K) = k1 compareTo k2
}
abstract class Node[K,V] extends TreeMap[K,V]
class InnerNode[K,V] extends Node[K,V]
class LeafNode[K,V] extends Node[K,V]
val root = new InnerNode[K,V]()
def search(n: Node[K,V], key: K): Option[(K,V)] = {
return n.find(_ == key)
}
def insert(key: K, value: V) { }
def delete(key: K) { }
}
}
import disttree._;
object Tester {
def main(args: List[String]) = {
var t = new Btree[Int, Any](2)
t.insert(1, "goodbye")
Console.println(t)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
常见错误。这是两个不同的
K
:使用
-explaintypes -uniqid
并显式传递参数,我得到:Common mistake. These are two different
K
's:Using
-explaintypes -uniqid
and passing the parameter explicitly, I get this:编译后,它呈现出两种变体,一种使用
Ordered
键 (K
),另一种使用键上的Ordering
:This compiles and it exhibits two variants, one using
Ordered
keys (K
) and the other using anOrdering
on the keys: