无法找到参数排序的隐含值

时间:2010-09-22 00:04:30

标签: scala scala-2.8 implicit-conversion type-parameter implicits

尝试编译时出现以下错误:

Btree.scala:9:错误:无法找到参数排序的隐含值:订购[K]         抽象类Node [K,V]扩展了TreeMap [K,V]

TreeMap应该接受我提供的隐式Ordering [A] val。也许隐式参数需要在Btree(TreeMap)实例化的对象测试器中? 我宁愿将隐式声明保留在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)
    }
}

2 个答案:

答案 0 :(得分:3)

这个编译并展示了两个变体,一个使用Ordered键(K),另一个使用键上的Ordering

package disttree {
    import scala.collection.immutable.TreeMap
    import scala.math.{Ordered, Ordering}

//  class Btree[K <% Ordered[K], V](fanout:Int) // Using Ordered
    class Btree[K : Ordering, V](fanout:Int)    // Using Ording
    {
    /* Using Ordered
        implicit object DefaultOrder extends Ordering[K] {
            def compare(k1: K, k2: K) = k1 compareTo k2
        }
    */

    /* Using Ordering */
        val keyOrdering = implicitly[Ordering[K]]

        implicit object DefaultOrder extends Ordering[K] {
            def compare(k1: K, k2: K) = keyOrdering.compare(k1, k2)
        }

        abstract class Node extends TreeMap[K,V]

        class InnerNode extends Node

        class LeafNode extends Node

        val root = new InnerNode()

        def search(n: Node, 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)
    }
}

答案 1 :(得分:3)

常见错误。这是两个不同的K

class Btree[K <: Comparable[K],V](fanout:Int) {
    // The K below is the type parameter of Btree
    implicit object DefaultOrder extends Ordering[K] {
        def compare(k1: K, k2: K) = k1 compareTo k2
    }

    // The K in the error is the type parameter of Node
    abstract class Node[K,V] extends TreeMap[K,V]

    // And this is yet another K
    class InnerNode[K,V] extends Node[K,V]

    // And yet another
    class LeafNode[K,V] extends Node[K,V]

使用-explaintypes -uniqid并明确传递参数,我得到了这个:

<console>:13: error: type mismatch;
 found   : Btree#31335.this.DefaultOrder#31343.type (with underlying type object Btree#31335.this.DefaultOrder#31344)
 required: Ordering#3222[K#31346]
               abstract class Node[K,V] extends TreeMap[K,V]()(DefaultOrder)
                                                               ^
Btree#31335.this.DefaultOrder#31343.type <: Ordering#3222[K#31346]?
  object Btree#31335.this.DefaultOrder#31344 <: scala.math.Ordering#11986[K#31346]?
    scala.math.Ordering#11986[K#31336] <: scala.math.Ordering#11986[K#31346]?
      K#31346 <: K#31336?