scala/cats2

kittens example

wefree 2025. 3. 5. 20:56

https://github.com/typelevel/kittens

 

"org.typelevel" %% "kittens" % "3.4.0"

 

Example1

import cats.Show
import cats.derived.semiauto
import cats.implicits.*

case class Name(value: String)
case class Person(name: Name, age: Int)

object Person {
  given Show[Person] = semiauto.show
}

object Test {
  def main(args: Array[String]): Unit = {
    val person = Person(Name("KJM"), 20)
    println(person.show)  // Person(name = Name(value = KJM), age = 20)
  }
}

 

Example2

import cats.Functor
import cats.derived.semiauto
import cats.implicits.*

case class Content[T](items: Seq[T])

object Content {
  given Functor[Content] = semiauto.functor
}

object Test {
  def main(args: Array[String]): Unit = {
    val content = Content[Int](List(1, 2))
    println(content.map(_ + 1)) // Content(List(2, 3))
  }
}