Skip to content

Defining val from ValDef #13042

Jul 9, 2021 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

So there are two parts to this answer -- use an inline block to remove a layer of indirection:

  inline def decorateVals[A](output: ValDefInspection => Unit)(inline block: A): A =
    ${ Impl.decorateValsImpl('output, 'block) }

and then define the macro with:

def decorateValsImpl[A: Type](output: Expr[ValDefInspection => Unit], block: Expr[A])(using Quotes): Expr[A] = {
      import quotes.reflect.*

      def rewriteBlock(data: Term): Term = {
        data match {
          case Block(stmts, expr) =>
            val newStmts = stmts.flatMap(rewriteStatement)
            Block(newStmts, expr)
        }
      }

      def rewriteStatement(statement: Statement): List[Statement] = {
       …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@nicolasstucki
Comment options

@wsargent
Comment options

Answer selected by wsargent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants