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

编程入门 行业动态 更新时间:2024-10-28 09:14:57
本文介绍了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”的成员。

本文发布于:2023-11-25 13:36:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员   错误   参数   类型   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!