Scala Language Macros

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

Macros are a form of compile time metaprogramming. Certain elements of Scala code, such as annotations and methods, can be made to transform other code when they are compiled. Macros are ordinary Scala code that operate on data types that represent other code. The [Macro Paradise][] plugin extends the abilities of macros beyond the base language. [Macro Paradise]: http://docs.scala-lang.org/overviews/macros/paradise.html

Syntax

  • def x() = macro x_impl // x is a macro, where x_impl is used to transform code
  • def macroTransform(annottees: Any*): Any = macro impl // Use in annotations to make them macros

Remarks

Macros are a language feature that need to be enabled, either by importing scala.language.macros or with the compiler option -language:macros. Only macro definitions require this; code that uses macros need not do it.



Got any Scala Language Question?