Scala REPL“错误:值>不是类型参数T”的成员。 [英] Scala REPL "error: value > is not a member of type parameter T"

查看:88
本文介绍了Scala REPL“错误:值>不是类型参数T”的成员。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的文件

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不会工作。但出现以下错误

I'm using the ":paste" method because :load doesn't work. But I get the following error

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

我确定此文件是正确的,因为它来自课程示例,并且在教授使用...时可以在课堂上使用。

I'm sure this file is correct, because it is from class examples, and it worked in class when Prof. is using...

有帮助吗?

推荐答案

您会收到此错误,因为并非每种类型的 T 已定义> < 等。

You get that error because not every type T has >,< etc. defined.

您可能想要的是 T 订购或隐式转换为 Ordered ,因此已全部定义。

What you probably wanted is T to be Ordered or be implicitly convertible to something that is Ordered , and therefore have all of them defined.

这应该可以修复错误消息:

This should fix the error messages:

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 的子类型或定义隐式转换。

T <% S (a view bound) says that type T must be convertible to S, so it has to be either a subtype of S or have implicit conversion defined.

此任务的接受者答案详细解释。

这篇关于Scala REPL“错误:值&gt;不是类型参数T”的成员。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆