简体   繁体   English

Scala匿名类类型不匹配

[英]Scala anonymous class type mismatch

I am creating a list holding Comparable objects and wish to create one object that serves as the minimum of the list, such that it always returns -1 for its compareTo method. 我正在创建一个保存Comparable对象的列表,并希望创建一个充当列表最小值的对象,这样它的compareTo方法始终返回-1。 Other methods in the list, like print here requires an input of type A. If I compile the code I get the following error: 列表中的其他方法(如此处的print需要输入A类型。如果我编译代码,则会出现以下错误:

error: type mismatch;
found   : java.lang.Object with java.lang.Comparable[String]
required: String
l.print(l.min)

Anyone have any idea about how can a create such a minimum element so that it is always smaller than any other elements in the list? 任何人都对如何创建这样一个最小元素有任何想法,以使其始终小于列表中的任何其他元素?

class MyList[A <: Comparable[A]] {
  val min = new Comparable[A] {
    def compareTo(other: A) = -1
  }

  def print(a: A) = {
    println(a)
  }
}

class Run extends Application {
  val l = new MyList[String]
  l.print(l.min)
}

Well, the input passed is not equal to the input provided, right? 好吧,传递的输入不等于提供的输入,对吗? print needs an A : print需要一个A

def print(a: A) = {

And min does not return an A : min不返回A

val min = new Comparable[A] {

As to creating such an A as you want it... how could you possibly go about it? 至于创建自己想要的A ...您怎么可能去做呢? You don't know anything about A -- you don't know what its toString returns, you don't know what methods it implements, etc. 您对A一无所知-您不知道它的toString返回什么,也不知道它实现了什么方法,等等。

So, basically, change your algorithm. 因此,基本上,改变您的算法。

You are getting a compile error because you're trying to use a Comparable where the compiler is expecting a A, what you really want to do is: 之所以会出现编译错误,是因为您尝试在编译器期望A的地方使用Comparable,所以您真正想要做的是:

  val min: A = new A {
    def compareTo(other: A) = -1
  }

but you can't do this in Scala (or Java), because you're trying to create an object of an unknown type (A). 但您无法在Scala(或Java)中执行此操作,因为您正在尝试创建未知类型(A)的对象。 You could do this using reflection, but you would still have the problem of creating an object which was less than any other object in the list. 可以使用反射执行此操作,但是仍然会遇到创建比列表中任何其他对象都要少的对象的问题。

Also, be aware that your implementation of compareTo will have problems with almost any sorting algorithm you choose, because you can't guarantee compareTo is always called from min. 另外,请注意,compareTo的实现几乎对您选择的所有排序算法都会有问题,因为您不能保证始终从min调用compareTo。 For example, you could get: 例如,您可以获得:

min.compareTo(list(0)) // returns -1
list(0).compareTo(min) // could be anything really

If you want a list that returns a specific object as the 'minimum' then you could just prepend a specific value to the sorted list: 如果您想要一个将特定对象作为“最小”值返回的列表,则可以在排序后的列表之前添加一个特定值:

class MyList2[A <: Comparable[A]] {
    val min: A; // somehow create an instance of the class A
    val list: List[A]

    def sort(fn: (A, A) => Boolean) = {
        min :: list.sort(fn)
    }
}

but as Daniel says, this is probably the wrong way to go about it. 但是正如丹尼尔所说,这可能是错误的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM