St.Antario
St.Antario

Reputation: 27455

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?

Upvotes: 1

Views: 176

Answers (1)

St.Antario
St.Antario

Reputation: 27455

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)
  )
}

Upvotes: 1

Related Questions