top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is Macro in Scala? What is the use of Macros in Scala?

+2 votes
384 views
What is Macro in Scala? What is the use of Macros in Scala?
posted Aug 17, 2016 by Joy Nelson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Our flavor of macros is reminiscent of Lisp macros, adapted to incorporate type safety and rich syntax. Unlike infamous C/C++ preprocessor macros,

Scala macros:

1) are written in full-fledged Scala,

2) work with expression trees, not with raw strings,

3) cannot change syntax of Scala.

Macros are functions that are called by the compiler during compilation. Within these functions the programmer has access to compiler APIs. For example, it is possible to generate, analyze and typecheck code.

Macros are shipping with the official Scala compiler. Since 2.10.0 Scala includes macros that can be enabled with import language.experimental.macros on per-file basis or with -language:experimental.macros on per-compilation basis. Numerous commercial and research projects are already using macros.

Macros are good for code generation, static checks and domain-specific languages, 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 a concise and maintainable way. Therefore we believe that macros are a valuable asset to the Scala programming language.

answer Aug 30, 2016 by Karthick.c
...