我的目标是在初始化包含所述对象的PriorityQueue时创建一个捕获2个或更多对象的varargs的函数.
相关代码是:
case class Topic(topic: String, usageFrequency: Long = 1)
object FreqOrdering extends Ordering[Topic] {
def compare(a: Topic, b:Topic) = -(a.usageFrequency compare b.usageFrequency)}
def initPriQu(a : Topic, b: Topic, c: Topic*): PriorityQueue[Topic] = {
return PriorityQueue(a,b,c)(FreqOrdering)}
Run Code Online (Sandbox Code Playgroud)
sbt(Scala 2)中的错误:
[错误]实测值:TopicTrenderInit.FreqOrdering.type
[错误]需要:scala.math.Ordering [等于]
[错误]注:TopicTrenderInit.Topic <:等于(和TopicTrenderInit.FreqOrdering.type <:scala.math.Ordering [TopicTrenderInit .Topic]),但特性排序在类型T中是不变的.
[错误]您可能希望调查通配符类型,如_ <: Equals.(SLS 3.2.10)
[错误]返回PriorityQueue中(A,B,C)(FreqOrdering)
[错误] ^
[错误] /home/aaron-laptop/Documents/Scala/topic_trender100/src/main/scala/main.scala :48:25:类型不匹配;
[错误]实测值:scala.collection.mutable.PriorityQueue [等于]
[错误]需要:scala.collection.mutable.PriorityQueue [TopicTrenderInit.Topic]
[错误]注:等于>:TopicTrenderInit.Topic,但类PriorityQueue是不变类型A.
[错误]您可能希望调查通配符类型,例如_ >: TopicTrenderInit.Topic.(SLS 3.2.10)
[错误]返回PriorityQueue(a,b,c)(FreqOrdering)
没有'*'表示vararg一切正常,没有错误.我认为最糟糕的是困扰我所需要的:scala.math.Ordering [Equals]错误我看到了.我还阅读了一篇关于模式匹配的文章,但我觉得我必须阅读更多内容才能理解实现.这里发生了什么?
谢谢.