Scala REPL“错误:值>不是类型参数T的成员”

时间:2013-05-12 09:43:47

标签: class scala compiler-errors

这是我的档案

trait Set[T] {
    def contains(x: T): Boolean
    def incl(x: T): Set[T]
    def union(that: Set[T]): Set[T]
}

class Empty[T] extends Set[T] {
    override def toString = "."
    def contains(x: T): Boolean = false
    def incl(x: T): Set[T] = new NonEmpty[T](x, new Empty[T], new Empty[T])
    def union(that: Set[T]): Set[T] = that
}

class NonEmpty[T](elem: T, left: Set[T], right: Set[T]) extends Set[T] {
    override def toString = "{" + left + elem + right + "}"

    def contains(x: T): Boolean =
        if (x < elem) left contains x
        else if (x > elem) right contains x
        else true

    def incl(x: T): Set[T] =
         if (x < elem) new NonEmpty(elem, left incl x, right)
         else if (x > elem) new NonEmpty(elem, left, right incl x)
         else this

    def union(that: Set[T]): Set[T] =
        ((left union right) union that) incl elem
}

我正在使用“:paste”方法,因为:加载不起作用。但是我收到以下错误

<console>:25: error: value < is not a member of type parameter T
               if (x < elem) left contains x
                     ^
<console>:26: error: value > is not a member of type parameter T
               else if (x > elem) right contains x
                          ^
<console>:30: error: value < is not a member of type parameter T
                if (x < elem) new NonEmpty(elem, left incl x, right)
                      ^
<console>:31: error: value > is not a member of type parameter T
                else if (x > elem) new NonEmpty(elem, left, right incl x)

我确定这个文件是正确的,因为它来自类示例,并且在教授使用时它在课堂上工作...

有帮助吗?

2 个答案:

答案 0 :(得分:5)

您收到该错误,因为并非所有类型T都定义了><等。

您可能希望T成为Ordered或隐式转换为Ordered,因此可以定义所有内容。

这应修复错误消息:

class NonEmpty[T <% Ordered[T]](elem: T, left: Set[T], right: Set[T]) extends Set[T] {
    override def toString = "{" + left + elem + right + "}"

    def contains(x: T): Boolean =
        if (x < elem) left contains x
        else if (x > elem) right contains x
        else true

    def incl(x: T): Set[T] =
        if (x < elem) new NonEmpty(elem, left incl x, right)
        else if (x > elem) new NonEmpty(elem, left, right incl x)
        else this

    def union(that: Set[T]): Set[T] =
        ((left union right) union that) incl elem
}

T <% S(视图边界)表示类型T必须可转换为S,因此它必须是S的子类型或已定义隐式转换

this queston的接受者答案更详细地解释了它。

答案 1 :(得分:0)

自&#34; view bounds&#34;已在Scala 2.11中弃用,这是使用&#34; context bounds&#34;的通用二进制搜索树的替代实现。如下:

object GenericBinarySearchTree {

abstract class Set[T] {
  def incl(x: T): Set[T]
  def contains(x: T): Boolean
  def union(that: Set[T]): Set[T]
}

type L[X] = X => Ordered[X]

class Empty[T : L] extends Set[T] {

  override def toString = "."

  def incl(x: T): Set[T] = new NonEmpty(x, new Empty, new Empty)

  def contains(x: T): Boolean = false

  def union(that: Set[T]): Set[T] = that
}

class NonEmpty[T : L](elem: T, left: Set[T], right: Set[T]) extends Set[T] {  

  override def toString = "{" + left + elem + right + "}"

  def incl(x: T): Set[T] = {
    if(x > elem) new NonEmpty(elem, left, right.incl(x))
    else if(x < elem) new NonEmpty(elem, left.incl(x), right)
    else this
  }

  def contains(x: T): Boolean = {
    if(x == elem) true 
    else if(x > elem) right.contains(x)
    else left.contains(x)
  } 

  def union(that: Set[T]): Set[T] = ((left union right) union that) incl elem
}

}