6. About Macros in General
• What is a macro
• Macro(Macroinstruction): An instruction to the
compiler, to replace one part of the program with a
replacement, created based on a
procedure/template
[
https://en.wikipedia.org/wiki/Macro_(computer_sci
ence) ]
7. About Macros in General
• Typical example: C preprocessor macros
• In C language, perhaps the most common macro is the debug macro
8. About Macros in General
• Typical example: C preprocessor macros
• In C language, perhaps the most common macro is the debug macro
• In C language, macro works with text substitution by the pre-compiler
9. About Macros in General
• Typical example: C preprocessor macros
• In C language, perhaps the most common macro is the debug
macro
• This illustrates the point from the definition we gave before
• In C language, macro works with text substitution by the pre-
compiler
• This also illustrates the problem with this kind of macros
10. About Macros in General
• This also illustrates the problem with this kind of
macros
11. About Macros in General
• This also illustrates the problem with this kind of
macros
12. Macros in Scala
• Experimental feature
• Originally introduced in v.2.10
• Gained a very high rate of adoption.
• Has been in active development/extention ever since
• Currently(2.12), still experimental
• Main researcher/developer behind scalamacros: Eugene Burmako, EPFL
• Resources
http://scalamacros.org/
http://docs.scala-lang.org/overviews/macros/overview.html (邦訳有り)
13. Macros in Scala
• In contrast to macros in C, Scala macros:
• 1) are written in full-fledged Scala,
• 2) work with expression trees, not with raw strings,
• 3) cannot change syntax of Scala.
• They get expanded at compile time, so they are statically checked, as will be
illustrated later
• Macros are called by the compiler during compilation, so they have access to the
compiler’s API (Compile-time Reflection)
• Using macros relies heavily on understanding Scala reflection
Resource: http://docs.scala-lang.org/overviews/reflection/overview.html
14. Macros in Scala
• But why do we need macros in the first place?
• [ http://scalamacros.org/paperstalks/2013-07-17-
WhatAreMacrosGoodFor.pdf ]
• Basically:
• Code Generation (& reduction of boilerplate code)
• Static Checks
• Domain-specific languages (DSLs)
• In this talk, we will demonstrate the 1st use
15. Macros in Scala
• Macros come in many flavors:
• Def macros
• Implicit Materializers
• Type providers
• Macro Annotations
• (still under rigorous extension)
• Here we will introduce the various concepts related to macros by examining types:
• Def macros, as the very very basic case of a macro
• Implicit Materializers, as the real use-case that turned up during work.
16. In Action: Code Sample
(master branch)
https://github.com/aalexelis/scalaMacro
Nyumon
17. Macros in Scala
• Macros is an experimental feature, must be imported
• A macro declaration is a method declaration, delegating to a macro
implementation, by the keyword macro
• Macros depend on a Context
• Context is …
• Blackbox / Whitebox contexts
• They bring reflection API into scope by importing universe
• The return type of a macro is of type Tree (AST: Abstract Syntactic Tree)
(more on this later)
18. Macros in Scala
• At the calling site, a macro invocation is a plain method call
• Macro implementation has to be already compiled when the macro calling code is
being compiled.
• Therefore, to set up SBT for macros we have to structure macro definitions and
macro invocations in separate projects
• Usually macros will need to have access to data structures and interfaces, so it is
better to include them (or make them depend on the “common” subsystem)
• Macro subsystem should declare dependency on scale-reflect module
• For some “more experimental” features, you must install the macro-paradise
compiler plugin
19. Macros in Scala
• Macro Bundles
• A new (2.11) convention regarding where to allocate the implementation
of a macro
• Before, macro implementation was represented as a function inside
an object it was declared
• With macro bundles, implementation can be a method in a trait that
extends the Macro trait, separately from the declaration site
• This has several advantages, mostly regarding modularity of the code,
and scope visibility
21. Macros in Scala
• Quasiquotes
• A new (2.11) way to define AST using string interpolation
• Before, AST had to be created “by hand”, from the basic
API elements provided by the reflection API
• With Quasiquotes, we can interpolate variables inside a
string that looks exactly how we want the macro
expansion to be
23. Macros in Scala
• Quasiquotes
• They can be used for pattern matching too, which
is very convenient
• Although the concept looks simple, its
implementation is probably complex, so the
scalamacros people warn that there might be still
residual bugs. In such case we have to resort
back to writing the AST by hand.
24. In Action: Code Sample
(cdef_macro branch)
https://github.com/aalexelis/scalaMacroNyum
on/tree/cdefext_macro
25. Typeclass
• A typeclass is a sort of interface that defines some
behavior. If a type is a part of a typeclass, that
means that it supports and implements the behavior
the typeclass describes.
• It means “in order to perform this operation in a
meaningful way, the type of the instance or
argument must be a member of the type class
26. Type Classes
• The idea of typeclasses is that you provide evidence
that a class satisfies an interface
• Instead of having the type directly implement the
interface, I can provide the implementation
elsewhere in the code
• This allow us avoid the inheritance spaghetti of
unrelated classes, and allows us to use different
libraries uniformly.
27. Notes on Reflection
• Reflection: the possibility that a program can have access, inspect and manipulate it’s
own representation
• Usually reflection is a “run-time” tool
• However、 Scala allows for a “compile-time” type of reflection, via macros
• They are based on the same api with the run-time reflection, which allows code
sharing
• Reflection Environment
• Universe: the environment info for current use of reflection
• Mirror: the parts we have access to reflectively
28. Notes on Reflection:
Types
• Types
• Types represent information about the type of a corresponding
symbol.
• They are available to the compiler, to be able to produce the
proper implementation of the program
• In general, they are erased after the type checking part of the
compilation is done
• However, when using reflections, we need access to type
information, in order to manipulate the entities properly.
29. Notes on Reflection:
TypeTags
• TypeTags: what are they?
• They are a type of manifest, of all the information the compiler knows
about a specific type
• They can be passed as parameters to methods, and used to extract type
information related to the manipulated entities
• In Scala reflection there are 3 types of TypeTags
• TypeTags
• WeakTypeTags
• ClassTags
30. Notes on Reflection: Symbols
• Symbols
• Symbols bind entities to names, so that we can refer to them. Whatever you can name in
Scala gets an associated Symbol
• TypeSymbol: symbol for a type in general
• ClassSymbol: symbol for a class or trait
• TermSymbol: symbol for val, var, def, object, package, value parameters
• MethodSymbol: symbol for methods
• ModuleSymbol: symbol for object declarations
• NoSymbol: top level symbol owner, (something like Nil for symbols)
• Free Symbols (@ look at reification for more info)
31. Notes on Reflection:
Trees
• Trees: abstract syntax trees (ASTs) represent programs
• Annotations
• reify
• Macros
• Subcategories of Trees: trees are made up of nodes
• TermTree: represent terms: Apply nodes for method invocations, New nodes for instantiations
etc.
• TypTree: represent types: ???
• SymTree: represent definitions: ClassDef for definition of class,, ValDef for definition of val, etc
• …some other type of short-lived tree types also exist