Scala Adding Macros to the Language

| by Josh Suereth Follow 0 Followers on Jul 13, 2012. Estimated reading time: 3 minutes |

The team behind Scala is adding an experimental version of macros in the forthcoming release version 2.10. Scala macros provide an advanced form of compile-time meta-programming. The Scala macros website states:

Macros significantly simplify code analysis and code generation, which makes them a tool of choice for a multitude of real-world use cases. Scenarios that traditionally involve writing and maintaining boilerplate can be addressed with macros in concise and maintainable way. Therefore we believe that macros are a valuable asset to Scala programming language.

Scala's macros allow developers to create methods whose implementation is a syntax tree transformation. These are standard method definitions that are transparently transformed during compilation. A simple example is the assert method:

import scala.reflect.makro.Context
import language.experimental.macros
object Asserts {
   def assert(cond: Boolean, msg: Any) = macro Asserts.assertImpl
   def raise(msg: Any) = throw new AssertionError(msg)
   def assertImpl(c: Context)(cond: c.Expr[Boolean], msg: c.Expr[Any]): c.Expr[Unit] =
         if(assertionsEnabled) c.reify(if(!cond.splice) raise(msg.splice))
          else c.reify(())

The assert macro appears as a normal method in code. The implementation uses the macro method to delegate to a compiler extension. The compiler extension is the method assertImpl. assertImpl takes as parameters the current compilation context and the arguments to assert as syntax trees (Expr). These syntax trees are then used to generate a new syntax tree that is inserted at the location of the assert macro method call.

For the assert macro, a method call of assert(x != null, "X is null") will fill the cond variable with the syntax tree for x != null and the msg variable with the syntax tree for "X is null". The call to reify generates a new syntax tree of if(x != null) Asserts.raise("X is null") or (). This syntax tree replaces the orignal assert(x != null, "X is null") call.

reify and the self cleaning macro system are defined in more detail in the self cleaning macros proposal.

The addition of macros was considered dubious by some. In a blog post, entitled Scala Macros: "Oh God Why?", Jay Kreps states:

This was my reaction to the Scala macros proposal too. Not because there is anything necessarily bad about macros or the proposal, but just because - is this really the most critical thing?

Kreps proceeds to list a set of more important issues that include compilation speed, IDE support, documentation and compilation size.

Kreps was not alone in concerns. Ivan Todoroski writes, in a letter to the scala mailing list:

scala-macros look like a low-level hackish solution in search of a problem. They are complex to write, un-scalaish, difficult to debug, and will probably just add to Scala's image of "too much complex inscrutable magic". 

To which Martin Odersky, inventor of Scala, responds:

The intention of macros, and of Scala's language design in general, is to simplify things. We have already managed to replace code lifting by macros, and hopefully other features will follow. For instance, there was a strong push to somehow eliminate the implicit parameter in atomic { implicit transaction => ... } and several other related situations. With macros this is trivial. 

The debate over Scala macros has died down as the community waits for a look at the final implementation. A final release has not stopped the adventurous from trying a hand at macros. Several community macro based projects have sprouted, including:

  • Macrocosm - A library experimenting with practical use cases in macros.
  • Expecty - An adaptation of assert statements in Groovy's Spock into Scala.
  • Slick - An effort to bring LINQ-like database library. Slick can convert Scala syntax into database queries.
  • ScalaMock - A mock object testing library for Scala.

A list of other features coming in Scala 2.10 can be found in the 2.10.0-M4 release notes and include:

While the Scala 2.10.0 final release is rapidly approaching, the Scala team is asking for folks to experiment with the latest milestone release and provide feedback. You can download the latest milestone from here.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Macros in the stdlib by Daniel Sobral

It would have been interesting to also point out some of the methods in the stdlib that are being implemented with macros, such as the type-safe formatted interpolated string.

Scala is the kitchen sink of programming languages by Ronald Miura

How many programming language features are still NOT present in Scala?

Re: Scala is the kitchen sink of programming languages by Faisal Waris

Having too many features in a language by itself is not an issue (except for the implementers, perhaps).

It would be an issue if newcomers to a language have to learn the features all at once.

There is an idea of Scala 'profiles' whereby companies can start with lower profiles and gradually move to higher ones as they gain more experience.

Actually, no. by Steve McJones

Although I recognize that this is more a less a cheap stab at the language, I would say that Scala allows you to solve a lot of "real world" problems without all the kitchen sink-attitude and the complexity of C++, C# or "Enterprise" Java.

Considering how much skepticism the idea received from the community in the first place, the scarcity of criticism tells a lot about the quality of the design, the execution and the implementation.

Re: Actually, no. NO by Paul Anders

That "kitchen sink-attitude" you are referring to does not comes from java language features, they stem from java 'enterprise' frameworks and libraries. And those are already invading scala too. Look at those sprinkling dependency injection frameworks for example...

Re: Scala is the kitchen sink of programming languages by Christian Helmbold

Would this really solve the problem? What about new employees joining a company which uses a high profile? What about external libraries and frameworks? No, I think profiles don't solve the problem. Profiles are a sign of an underlying problem.

Re: Scala is the kitchen sink of programming languages by Alexandre Poitras

You can specify which features are used for a given class. Some high features should be reserved for libraries/framework designers.
I think macros are a great example of that. They won't be used by most application developers but will be of great use to libraries makers. I love the idea of a language à la carte. Give you the chance to adapt your use of the language depending of the package you are working in.

Nice by Lyndon Samson

Very nice, one of the few modern languages with this facility (boo being the other one). Won't be written by 95% of developers, but will make extending the language and DSL writing much more powerful.

Free Scala course from the man himself by Faisal Waris

PS. This is another internet revolution brewing. Free online courses with class sizes approaching 100,000+ students worldwide.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

9 Discuss