简体   繁体   中英

Using cats.Contravariant typeclass

I'm trying to apply my Contravariant typeclass with syntax and faced the issue that it is not found. Here is what I currently have:

import cats._
import cats.implicits._

object Test {
  type Foo[A] = A => Unit
  private val f: Foo[String] = (_: String) => ()
  implicit val cvar: Contravariant[Foo] = null

  private val FF: Foo[Int] = f.contramap((i: Int) => //error: value contramap is not a member of Foo
    String.valueOf(i)
  )
}

I don't understand it. I provided implicit Contravariant[Foo] , but the syntax is not applied anyway. What's wrong?

The mistake was that I did not extend the ContravariantSyntax . Removing implicit and mixing in it works as expected:

import cats._
import cats.syntax.ContravariantSyntax

object Test extends ContravariantSyntax{
  type Foo[A] = A => Unit
  private val f: Foo[String] = (_: String) => ()
  implicit val cvar: Contravariant[Foo] = null
  private val FF: Foo[Int] = f.contramap((i: Int) =>  //compiles - Ok!
    String.valueOf(i)
  )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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