1

I was trying to create a new type class instance of Semigroup in Cats 2.7.0 for a case class called Expense. The problem occurs when the compiler throws an error telling me that the value instance does not exist for cats.kernel.Semigroup. Also, intellisense autocompletes all the other methods but the instance one.

I thought this could be done by using the instance method of the companion object of the Semigroup class, but it appears that it does not exists.

This is the code bellow:

object Semigroups extends App:

  import cats.Semigroup       // Type class import

  // Expense class
  case class Expense(id: Long, amount: Double)

  // Creating the type class instance:
  given expenseSemigroup: Semigroup[Expense] =

    Semigroup.instance[Expense]( // <- the error ocurrs here
      (exp1, exp2) =>
        Expense(exp1.id + exp2.id, exp2.amount + exp1.amount)
    ) 

end Semigroups

Maybe this method is depreciated now? What is the new one if it exists?

That's all, hope I made myself clear with this. Have everyone a nice day!

  • 1
    Can't reproduce: https://scastie.scala-lang.org/BalmungSan/fsjNeemQTZClVGe1oMBjzw/2 it is probably just your IDE, remember to always confirm using a real compiler. – Luis Miguel Mejía Suárez Feb 17 '22 at 01:11
  • @LuisMiguelMejíaSuárez Thanks for the advice, I was thinking about using the scala3 compiler but I have an strange error that does not let me use it at all. Although I was not aware of Scastie, I'm going to use it if I saw another compiler related issue. Anyways, thank you so much! – Gabriel Santana Paredes Feb 18 '22 at 07:16
  • 1
    When I say real compiler I mean through a build tool like **sbt**, not manually invoking the compiler. My point is that IDEs can give you false positives. – Luis Miguel Mejía Suárez Feb 18 '22 at 13:27

0 Answers0