Type mismatch; Found: Int (1) Required: B

I am trying to extend a class Listto give it an easier way to compare sizes, however I ran into a header error ...

Here is my code:

implicit class RichList[A, B](input: List[A]) {
  def >(that: List[B]): Boolean = input.size > that.size
  def <(that: List[B]): Boolean = input.size < that.size
}

The idea was that since all this compares the sizes of lists, their types may be different, and that doesn't matter, however, when I try to do this:

val test = List(1,2,3,4) < List(1,2,3,4,5)

I get the above error. If I remove B and set it thatas a type List[A], it works fine, but then I cannot use lists containing 2 different types ...

Why can't A and B be of the same type? Or am I missing something?

Edit: Good. I found a solution to this error, which is pretty simple:

implicit class RichList[A](input: List[A]) {
  def >[B](that: List[B]): Boolean = input.size > that.size
  def <[B](that: List[B]): Boolean = input.size < that.size
}

However, my question is still standing; why can't i do it another way?

+4
1

B . > <.

.

implicit class RichList[A](input: List[A]) {
  def >[B](that: List[B]): Boolean = input.size > that.size
  def <[B](that: List[B]): Boolean = input.size < that.size
}

, -, .

List(1,2,3) > List("1", "2")

, ( )

new RichList[Int, B](List[Int](1,2,3)).>(List[String]("1", "2"))

B String. .

+6

Source: https://habr.com/ru/post/1535334/


All Articles