Ali Tc
Ali Tc

Reputation: 7

Set up and use the Cats library for typeclass derivation in my Scala 3.4.2 project

Good Day, I am using Intellij 2024.1.3 and Oracle OpenJDK 22.0.1

this is my build.sbt:

ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "3.4.2"
lazy val root = (project in file("."))
    .settings(
        name := "untitled1",
        libraryDependencies ++= Seq(
          "org.typelevel" %% "cats-core" % "2.10.0",
          "org.typelevel" %% "cats-derivation" % "2.6.1"
        )
      )

and this is my program:

import cats.Show
import cats.derived._

final case class Invoice
    (
    id: String,
    number: Option[String],
    amount: Double
    )

object Invoice {
    given Show[Invoice] = derive.show[Invoice]
}

@main def main(): Unit = {
  val invoice = Invoice("1", Some("INV-001"), 100.0)
  println(Show[Invoice].show(invoice)) // Prints the Invoice using Show instance
}

in the "given" line the keyword "derive" is red. I tested it with all different combinations but it is hopelessly difficult to find the right way. Could you please help.

Upvotes: 0

Views: 74

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51648

I don't know where you got "org.typelevel" %% "cats-derivation" % "2.6.1". Maven doesn't know it https://search.maven.org/search?q=cats-derivation on contrary to for example cats-core https://search.maven.org/search?q=cats-core

I suspect you're using https://github.com/typelevel/kittens

Replace the dependency with

"org.typelevel" %% "kittens" % "3.3.0"

and derivation with

given Show[Invoice] = semiauto.show[Invoice]

Upvotes: 4

Related Questions