
Type parameters in an implementation may come with WeakTypeTag context bounds. If a macro implementation has type parameters, actual type arguments must be given explicitly in the macro definition’s body. Macro definitions and macro implementations may both be generic. Your users, however, don’t need to enable anything - macros look like normal methodsĪnd can be used as normal methods, without any compiler switches or additional configurations. Or with -language:experimental.macros (providing a compiler switch) on per-compilation basis. So in order to write macros you need to enable them.ĭo that either with import on per-file basis The result type of the assertImpl macro implementation is again a wrapped tree, of type c.Expr.Īlso note that macros are considered an experimental and advanced feature, Expr is a type defined in Context that wraps an abstract syntax tree of type T. But where the original macro parameter has type T, a macro implementation parameter has type c.Expr. This is followed by a list of parameters that have the same names as the macro definition parameters. First comes a single parameter, of type. (cond: c.Expr, msg: c.Expr) : c.Expr =Īs the example shows, a macro implementation takes several parameter lists. Import ĭef raise(msg: Any) = throw new AssertionError(msg) Our macro workshop for more in-depth examples. That dramatically simplify writing macros. Nevertheless, this guide is not obsolete -Įverything written here will still work in both Scala 2.10.x and Scala 2.11.x, so it will be helpful to read it through.Īfter reading the guide, take a look at the docs on quasiquotesĪnd macro bundles to familiarize yourself with latest developments So naturally the contents of the document are outdated.
#Macro definition update
UPDATE This guide has been written for Scala 2.10.0, and now we’re well into the Scala 2.11.x release cycle, Which have either been removed in Scala 3 or replaced by an alternative.ĭef macros are shipped as an experimental feature of Scala since version 2.10.0.Ī subset of def macros, pending a thorough specification, is tentatively scheduled to become stable in one of the future versions of Scala.

Remember that case is significant in macro names.This doc page is specific to features shipped in Scala 2, In fact, the implementation defines the arguments as simple macros.


Within the replacement text, the arguments can be substituted by the form: $A.

The name of the macro must begin with a letter and can consist of any combination of letters and numbers. This can be used in more complex situations. #define NAME( a' b' c') - defines a macro with arguments. More flexibility can be obtained by using a macro with arguments, described below. This form is limiting, in that the variable names are fixed. This is similar to, but independent of, the macro system in the score language. This can save typing, and can lead to a coherent structure and consistent style. The orchestra macro system in Csound is a very simple one, and uses the characters # and $ to define and call macros. Macros are textual replacements which are made in the orchestra as it is being read.
