Why we can't have the two issues about variance arisen in book Scala with Cats achieved both. Give me an example for each issue

43 views Asked by At

Scala with Cats arise two issues as follow: enter image description here And give the answer: enter image description here

But it seems that it could, like:

sealed trait A
final case object B extends A
final case object C extends A

trait Printer[-T]:
  def print(t: T): Unit

implicit val aPrinter: Printer[A] = (t: A) => println(s"a printer print $t")

implicit val bPrinter: Printer[B.type] = (t: B.type) => println(s"b printer print $t")

// implicit val cPrinter: Printer[C.type] = (t: C.type) => println(s"c printer print $t")

object PrinterUse:
  def print[T](t: T)(using printer: Printer[T]): Unit =
    printer.print(t)

@main
def main():Unit =
  // issue 2
  PrinterUse.print(B)
  // b printer print B

  // issue 1
  PrinterUse.print(C)
  // a printer print C

I think I misunderstood some content.

I try to write example as above, and it works fine.

0

There are 0 answers