【问题标题】:Scala REPL "error: value > is not a member of type parameter T"Scala REPL“错误:值 > 不是类型参数 T 的成员”
【发布时间】:2013-05-06 13:01:54
【问题描述】:

这是我的文件

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" 方法,因为 :load 不起作用。但我收到以下错误

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

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

有什么帮助吗?

【问题讨论】:

    标签: class scala compiler-errors


    【解决方案1】:

    您会收到该错误,因为并非每个类型 T 都定义了 &gt;&lt; 等。

    您可能想要的是 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 &lt;% S(视图绑定)表示T 类型必须可转换为S,因此它必须是S 的子类型或已定义隐式转换。

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

    【讨论】:

      【解决方案2】:

      由于“view bounds”在 Scala 2.11 中已被弃用,使用“context bounds”的通用二叉搜索树的替代实现如下:

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

      【讨论】:

        猜你喜欢
        • 2015-05-01
        • 2013-05-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2019-02-03
        • 2023-03-09
        • 1970-01-01
        • 2015-10-03
        相关资源
        最近更新 更多