Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monoid derivation applies requires Monoid for type parameter even when unnecessary #546

Closed
mrdziuban opened this issue Dec 8, 2022 · 4 comments · Fixed by #625
Closed

Comments

@mrdziuban
Copy link

At least on scala 3 (haven't tested on scala 2) a derived Monoid[F[A]] requires a Monoid[A] even if A is only referenced within types that have a MonoidK instance, i.e. that can form a Monoid regardless of their type parameter.

Could it instead require any number of Monoid[B]s where B is the full type of the each field that references A? For example:

case class Test[A](l: List[A], v: Vector[A]) derives Monoid

// generated before
given derived$Monoid[A](using m: Monoid[A]): Monoid[Test[A]]
// generated after
given derived$Monoid[A](using m1: Monoid[List[A]], m2: Monoid[Vector[A]]): Monoid[Test[A]]

Full example:

https://scastie.scala-lang.org/mrdziuban/wy1ITtp9SCCyUe7uUgfisA/4

import cats.Monoid
import cats.derived.*

case class Test[A](l: List[A]) derives Monoid

Monoid[Test[Boolean]]
/*
No given instance of type cats.kernel.Monoid[Playground.Test[Boolean]] was found for parameter ev of method apply in object Monoid.
I found:

    Playground.Test.derived$Monoid[Boolean](
      /* missing */summon[cats.kernel.Monoid[Boolean]]
    )

But no implicit values were found that match type cats.kernel.Monoid[Boolean].

One of the following imports might fix the problem:

  import cats.derived.auto.commutativeMonoid.given_CommutativeMonoid_A
  import cats.derived.auto.monoid.given_Monoid_A
*/
@joroKr21
Copy link
Member

joroKr21 commented Dec 8, 2022

Yeah I think that comes from Scala 3 and we can't control it 😞

@joroKr21
Copy link
Member

joroKr21 commented Dec 8, 2022

The workaround is to add the instance to the companion object explicitly:

object Test:
  given [A] Monoid[Test[A]] = semiauto.monoid

@mrdziuban
Copy link
Author

Oooh interesting and good to know! Feel free to close this then since it's not a kittens issue

@joroKr21
Copy link
Member

Maybe we should document this in the README so I will keep it open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants