Macro annotations replacement in Scala 3


Sorry if this question has already been discussed here.

Is there an officially recommended way migrate libraries with macro annotations to Scala 3?

In Scala 2 I use annotations to analyze case class field and generate helper methods in companion object. Library example.

In Scala 3 I know great way to replace annotations in case of typeclass derivation but what about just code generation?

Use case to make it clear:

@Validation // <- my macro annotation
case class Test(a: Int, b: Int Refined Positive, c: Int Refined Negative)
object Test {
  // helper method with arguments constructed after case class fields analysis
  def create(a: Int, b: Int, c: Int): Either[List[String] Refined NonEmpty, Test] = {
    // some code generated with my macros, beside the point
1 Like

You can try to use

For example simulacrum => simulacrum-scalafix. This is mentioned in Scala 3 migration guide.

This approach works if code generation at pre-compile time is enough and if limitations are not critical for a use case. Code generation at compile time is not possible yet (although using semanticdb can sometimes transfer things from compile time to pre-compile time).

1 Like

Thank you for reply!

Yes, I already seen simulacrum-scalafix project but I was hoping that there is a more convenient way to do it than pre-compile code generation :pensive:

I think maybe compiler plugin is what you want. Though it’s hard to write.
plugin example

1 Like

One more use case besides Simulacrum-scalafix: Breeze uses sbt plugin and Scalameta for source code generation as a Scala 3 replacement for macro annotation @expand