Skip to main content

A First Look at Scala Macros

Posted by cayhorstmann on January 14, 2013 at 9:32 PM PST

The final version of Scala 2.10 was released on January 4, 2013. Martin Christensen, a visiting scholar in our department, and myself have been playing with some of the new features, and I'll be blogging about some of our discoveries in my copious spare time.

Today, I'll show you how to write a simple macro in Scala. You may have seen macros in C, such as #define swap(x, y) { int temp = x; x = y; y = temp; }

C macros are just text substitutions. If you call swap(first, last), the result is