'Method 'read' overrides nothing

I am new to Scala Type Parameter

Below is the scala code,

  trait Pet {
    val name: String
  }

  class Cat(val name: String) extends Pet
  class Dog(val name: String) extends Pet
  trait Reader[T <: Pet]
  case class FileReader[T <: Pet](t : T) extends Reader[T]
  case class URLReader[T <: Pet](t : T) extends Reader[T]

  trait Process[R <: Reader[_]] {
    def read[T <: Pet](reader: Reader[T]):Seq[T]
  }

  val file= new Process[FileReader[_]]() {
    override def read[T <: Pet](reader: FileReader[T]): Seq[T] = {
      Seq.empty
    }
  }

  val url= new Process[URLReader[_]]() {
    override def read[T <: Pet](reader: URLReader[T]): Seq[T] = {
      Seq.empty
    }
  }
}

Why can't I pass FileReader and URLReader to read() function?

It shows the error as Method 'read' overrides nothing



Solution 1:[1]

Why can't I pass FileReader and URLReader to read() function?

You can, because they are both compatible with Reader[T]:

def read[T <: Pet](reader: Reader[T]): Seq[T]

What you can't do is override a function that takes Reader[T] with a function that takes FileReader[T] because the compiler can't guarantee that read is only ever called with instances of FileReader[T].

Solution 2:[2]

It seems you are a bit confused about how to abstract over a type of kind * -> * like Reader

This is the fix you need:

trait Process[R[x <: Pet] <: Reader[x]] {
  def read[P <: Pet](reader: R[P]): List[P]
}

You can see the code running here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim
Solution 2 Luis Miguel Mejía Suárez