INTRODUCTION TO SCALA MACROS
Jyotsna Karan
Software Consultant
Knoldus Software LLP
AGENDA
➔
What are scala macros
➔
Where macros are used
➔
How to write scala macros
➔
Types of macros
➔
Demo
➔
Scala macros were introduced in 2013
➔
They are experimental
➔
Written in Scala
➔
The macros are function : code => code
➔
They are like normal functions in look
➔
We already have macros in other languages as well: C, C++,
LISP etc.
What are Scala Macros
Terminologies that need to be familiar before learning scala :
➔
AST
➔
Run-time reflection
➔
DSLs
What are Scala Macros
➔
Remove redundant code
➔
Replace run-time reflection
➔
Generates type-checked code
➔
Simplify compiler in long run
What they good for?
AST
Run-time reflection
DSLs
What are its cons?
➔
Slick
➔
Akka
➔
MacWire
➔
Expecty
➔
Async
Where macros are used?
➔
Macros needs scala-reflect.jar in library dependencies.
➔
The separate compilation restriction requires macros to be
placed in a separate project
How to write macros?
➔
Imports needed for macros
import language.experimental.macros
import reflect.macros.Context
➔
Syntactical structure of macro
def myFirstMacro():R = macro firstImpl
def firstImpl(c : Context) : c.Expr[R] = { 
/* ...... */
}
Def Macros
➔
Context: It is basically the entry point for the macros invocation
➔
Reify: It is used to return the abstract syntax tree
Def Macros
➔
MyFirstMacro will print “hello Macro”
➔
Implementation of this example macro would be like :
def firstImpl(c : Context) : c.Expr[R] = { 
 reify {
println(“hello Macro”)
 }
}
Def Macros
➔
They are used with the type classes
➔
Available since 2013
Type classes: They are just pattern implemented with traits
trait Showable[T] { def show(x: T): String }
def show[T](x : T)(implicit s: Showable[T]) =
s.show
Implicit Macros
➔
We can provide default implementation for the type
trait Showable[T] { def show(x: T) }
object Showable {
implicit materialize[T]: Showable[T] = {
/* macro ..... */
}
}
Implicit Macros
➔
Textual representations
➔
Any definitions can be annotated
➔
Present in macro paradise library
➔
Static Annotation is the Class need to be extended to use
annotations
Macros Annotations
➔
http://docs.scala-lang.org/overviews/macros/overview.html
➔
https://en.wikipedia.org/wiki/Abstract_syntax_tree
➔
http://www.warski.org/blog/2012/12/starting-with-scala-macros-
a-short-tutorial
➔
https://en.wikipedia.org/wiki/Abstract_syntax_tree
References
Thank You...

Introduction to Scala Macros

Editor's Notes

  • #10 To enable the feature explicitly import are needed At first glance macro definitions are equivalent to normal function definitions, except for their body, which starts with the conditional keyword macro and is followed by a possibly qualified identifier that refers to a static macro implementation method. If, during type-checking, the compiler encounters an application of the macro m(args), it will expand that application by invoking the corresponding macro implementation method, with the abstract-syntax trees of the argument expressions args as arguments. The result of the macro implementation is another abstract syntax tree, which will be inlined at the call site and will be type-checked in turn. where c is a context argument that contains information collected by the compiler at the call site, Expression of same return by should be used Expr -> Scalatype + AST
  • #11 Reify is itself a macro to generate macro
  • #12 Reify is itself a macro to generate macro
  • #13 Reify is itself a macro to generate macro
  • #14 Reify is itself a macro to generate macro
  • #15 Reify is itself a macro to generate macro