Since 2.10.0 Scala includes macros. I was talk about something like the following:
* What do macros benefit to us?
* What can we do with macros?
* Actual use cases
19. Implementation of the macro
• Takes several parameter lists
First comes a single
parameter, of type
Context
20. Implementation of the macro
• Takes several parameter lists
Macro definition
Next, followed by a list of
parameters that have the
same names as the macro
definition parameters
21. Implementation of the macro
• The original macro parameter has
• type T
• A macro implementation parameter has
• type c.Expr[T]
22. Quasiquotes
• q"..." string interpolators that build code
• Unnecessary to directly implement the AST
• To use the quasiquotes from the macro, just write
import c.universe._
30. Implementation of the macro
• Type tags(actual type arguments) will be passed
along when the macro is expanded
31. Implementation of the macro
• Type tags(actual type arguments) will be passed
along when the macro is expanded
Come with
WeakTypeTag
context bounds
35. Why distinction?
• Type specification of macro is curious
class Foo
class Bar extends Foo
Scala 2.10
!
object FooExample {
def foo: Foo = macro foo_impl
def foo_impl(c: Context): c.Expr[Foo] =
c.Expr[Foo](c.universe.reify(new Bar).tree)
}
36. Why distinction?
scala> FooExample.foo
res0: Bar = Bar@4118f8dd
• Type checking during macro expansion
• Not affect after expansion
37. Blackbox restrictions
• Type parameters of macro affect the type of after
macro expansion
• When blackbox macro is used, techniques such as
Implicit Macros will not work
38. No restrictions Whitebox
• Same as 2.10 def macros
• Everything that could be done with macros in 2.10
should be possible in 2.11
47. Current state
• Optimized towards compiler developers, not library
users
Complicated!!
48.
49. Scala 2.12 (plan)
• Not introduce new features
• Bugfixes and stability improvements
• Mention later why reason…
50. scala.meta
• A new experimental API for metaprogramming
• Formerly known as Project Palladium
51. scala.meta
• Implemented in a library shipped separately from
the official Scala distribution
• The first milestone release is scheduled for this fall
52. The goal of scala.meta
• Metaprogramming easy
• New API is going to greatly simplify writing
macros
• Not require knowledge of compiler internals
53. Language model
• All represented with trees
Types
Modifiers
…
Names
Trees
scala.meta
Terms
Symbols
54. Language model
• Keeps all the information about the program
• Comments also remain
• No information is lost anymore