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

2019-09-02 17:24发布

这是我的文件

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
}

我使用了“:贴”的方法,因为:负载不工作。 但我得到以下错误

<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)

我敢肯定,这个文件是正确的,因为它是从类的例子,当教授利用在课堂上它的工作...

任何帮助?

Answer 1:

你得到这个错误,因为不是每个类型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或已隐式转换定义。

承诺人回答这个queston解释了它的更多细节。



Answer 2:

由于“ 视图边界 ”已在斯卡拉2.11被弃用,使用“通用二叉搜索树的替代实施背景下边界 ”低于:

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
}

}


文章来源: Scala REPL “error: value > is not a member of type parameter T”