# Macro annotations replacement in Scala 3

**URL:** <https://users.scala-lang.org/t/macro-annotations-replacement-in-scala-3/7374>\
**Category:** Question\
**Created:** [April 21, 2021, 8:24pm UTC](https://users.scala-lang.org/t/macro-annotations-replacement-in-scala-3/7374 "2021-04-21T20:24:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![poslegm](https://users.scala-lang.org/user_avatar/users.scala-lang.org/poslegm/32/1847_2.png) [@poslegm](https://users.scala-lang.org/u/poslegm)\
**Post date:** [April 21, 2021, 8:24pm UTC](https://users.scala-lang.org/t/macro-annotations-replacement-in-scala-3/7374/1 "2021-04-21T20:24:30Z")

</div>

Hello!

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](https://github.com/poslegm/brief).

In Scala 3 I know great way to replace annotations in case of [typeclass derivation](https://dotty.epfl.ch/docs/reference/contextual/derivation.html) 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)
// MACRO GENERATED CODE START
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
  }
}
// MACRO GENERATED CODE END
```

---

<div class="post-metadata">

**Author:** ![DmytroMitin](https://users.scala-lang.org/user_avatar/users.scala-lang.org/dmytromitin/32/1423_2.png) [@DmytroMitin](https://users.scala-lang.org/u/DmytroMitin)\
**Post date:** [April 21, 2021, 8:51pm UTC](https://users.scala-lang.org/t/macro-annotations-replacement-in-scala-3/7374/2 "2021-04-21T20:51:03Z")

</div>

You can try to use

> **[Scalameta · Library to read, analyze, transform and generate Scala programs](https://scalameta.org/)**
>
> Library to read, analyze, transform and generate Scala programs

> **[Scalafix · Refactoring and linting tool for Scala](https://scalacenter.github.io/scalafix/)**
>
> Refactoring and linting tool for Scala

For example [simulacrum](https://github.com/typelevel/simulacrum) =\> [simulacrum-scalafix](https://github.com/typelevel/simulacrum-scalafix). This is mentioned in [Scala 3 migration guide](https://scalacenter.github.io/scala-3-migration-guide/docs/macros/macro-libraries.html#macro-annotations-libraries).

This approach works if code generation at pre-compile time is enough and if [limitations](https://scalacenter.github.io/scalafix/docs/developers/symbol-information.html#known-limitations) are not critical for a use case. Code generation at compile time is not possible yet (although using [semanticdb](https://scalameta.org/docs/semanticdb/guide.html) can sometimes transfer things from compile time to pre-compile time).

---

<div class="post-metadata">

**Author:** ![poslegm](https://users.scala-lang.org/user_avatar/users.scala-lang.org/poslegm/32/1847_2.png) [@poslegm](https://users.scala-lang.org/u/poslegm)\
**Post date:** [April 22, 2021, 8:30pm UTC](https://users.scala-lang.org/t/macro-annotations-replacement-in-scala-3/7374/3 "2021-04-22T20:30:25Z")

</div>

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 😔

---

<div class="post-metadata">

**Author:** ![liewhite](https://users.scala-lang.org/user_avatar/users.scala-lang.org/liewhite/32/1872_2.png) [@liewhite](https://users.scala-lang.org/u/liewhite)\
**Post date:** [June 19, 2021, 8:22am UTC](https://users.scala-lang.org/t/macro-annotations-replacement-in-scala-3/7374/4 "2021-06-19T08:22:33Z")

</div>

I think maybe compiler plugin is what you want. Though it’s hard to write.  
[plugin example](https://github.com/liufengyun/scala3-plugin-example)

---

<div class="post-metadata">

**Author:** ![DmytroMitin](https://users.scala-lang.org/user_avatar/users.scala-lang.org/dmytromitin/32/1423_2.png) [@DmytroMitin](https://users.scala-lang.org/u/DmytroMitin)\
**Post date:** [November 23, 2022, 5:19am UTC](https://users.scala-lang.org/t/macro-annotations-replacement-in-scala-3/7374/5 "2022-11-23T05:19:56Z")

</div>

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`

> **[GitHub - scalanlp/breeze: Breeze is a numerical processing library for Scala.](https://github.com/scalanlp/breeze)**
>
> Breeze is a numerical processing library for Scala. - GitHub - scalanlp/breeze: Breeze is a numerical processing library for Scala.

> <https://github.com/scalanlp/breeze/blob/master/DEVELOP.md>

> **[GitHub - dlwh/sbt-breeze-expand-codegen: sbt plugin for doing Breeze's...](https://github.com/dlwh/sbt-breeze-expand-codegen)**
>
> sbt plugin for doing Breeze's codegen as an explicit source generation step. - GitHub - dlwh/sbt-breeze-expand-codegen: sbt plugin for doing Breeze's codegen as an explicit source generatio...
