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

macro implementations cannot have implicit parameters other than WeakTypeTag evidences #396

Open
vuhainguyen opened this issue Apr 24, 2022 · 4 comments

Comments

@vuhainguyen
Copy link

Hi, I tried to use some examples in https://github.com/softwaremill/magnolia/tree/scala2/examples/src/main/scala/magnolia1/examples with scala 2.13 but I got the compilation error below:

macro implementations cannot have implicit parameters other than WeakTypeTag evidences
  implicit def gen[T]: Print[T] = macro Magnolia.gen[T]

Did I miss out something or the issue is real?
Thanks in advance for your help.
Best regards,
Vu Hai

@adamw
Copy link
Member

adamw commented Apr 25, 2022

Hm did you use the exact same examples? They are part of the build so should work jsut fine. Can you post the full code that causes the problem?

@vuhainguyen
Copy link
Author

HI thanks for the feedback, I found this discussion https://gitter.im/propensive/magnolia?at=5e82194b49e7a8130cdaf16b, turns out I just need to add "org.scala-lang" % "scala-reflect" % "2.13.8" into my build.sbt for it to work with scala 2.13.
But my current project do cross compilation for both scala 2.13 and 3 and there's no scala reflect for scala 3. I noticed that the method Magnolia.gen is removed and we use the syntax given .. with for scala 3 as well.
Is there a way to make it work for both version of scala? (it's a bit out of the initial context of this thread, let me know if you want to close this one and open a new one instead 😅 )

trait RandomObjectDerivation {
  type Typeclass[A] = RandomObject[A]

  def join[A](caseClass: CaseClass[Typeclass, A]): Typeclass[A] = new Typeclass[A] {
    def generate(): A = caseClass.construct(_.typeclass.generate())
  }

  def split[A](sealedTrait: SealedTrait[Typeclass, A]): Typeclass[A] = new Typeclass[A] {
    def generate(): A = choice(sealedTrait.subtypes).typeclass.generate()
  }

  def split[T](ctx: SealedTrait[Typeclass, T]): Typeclass[T] =
    new Typeclass[T] {
      def generate(): T = choice(ctx.subtypes).typeclass.generate()
    }

  private def choice[A](xs: Seq[A]): A =
    xs.toIndexedSeq(Random.nextInt(xs.length))

  implicit def gen[A]: Typeclass[A] = macro Magnolia.gen[A]
}

@adamw
Copy link
Member

adamw commented Apr 25, 2022

Yes, in Scala 3 you can use implicits or .derived. Though I don't think you'll be able to compile the same codebase using both Scala 2 and Scala 3, there are some (not very significant, but still) differences.

I suppose you'll need to have some sources scala2 and some scala3 specific (see e.g. tapir's core module): https://github.com/softwaremill/tapir

@vuhainguyen
Copy link
Author

vuhainguyen commented Apr 25, 2022

Awesome, I followed this tutorial https://docs.scala-lang.org/scala3/guides/migration/tutorial-macro-cross-building.html
I can compile my project with both scala 2.13 and scala 3 now. Thanks a lot for your help 🙏
I can't find in the examples directory of scala 3 one example with .derived, can you give me one? 😅
I think this line implicit def gen[A]: Typeclass[A] = macro Magnolia.gen[A] helps me to generate the case class for scala 2.13, is there an equivalent for scala 3?

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

No branches or pull requests

2 participants