what's a macro?: learning by examples /...
DESCRIPTION
Presentation material by Ms. Takako Shimamoto at ScalaMatsuri 2014 http://scalamatsuri.org/en/TRANSCRIPT
![Page 1: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/1.jpg)
What's a macro?Learning by Examples
Takako Shimamoto BizReach, Inc
![Page 2: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/2.jpg)
What to Talk About
• What are macros?
• Good thing about macros
• Actual use cases of macros
• Future of macros
![Page 3: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/3.jpg)
Are you using macros?
If “Yes",
![Page 4: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/4.jpg)
The Fab You!
![Page 5: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/5.jpg)
Because
• Using macros is easy, developing macros is hard
![Page 6: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/6.jpg)
What are macros?
• Code that generates code
• Not textual code generation
![Page 7: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/7.jpg)
What are macros?
• An experimental feature of 2.10+
• Compiler invokes functions
![Page 8: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/8.jpg)
Before macros
• Ad-hoc textual code generation by sbt plugin
• Compile-time AST operation by compiler plugin
![Page 9: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/9.jpg)
Why macros are necessary?
• Code is made simple
• Efficiency improved
![Page 10: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/10.jpg)
Macro is on your
side!
![Page 11: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/11.jpg)
To use macros
• Need to be enabled explicitly
• import scala.language.experimental.macros
• -language:experimental.macros
![Page 12: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/12.jpg)
Notes
• Macros are separate compilation
• Macro implementations need to be compiled the main compilation
![Page 13: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/13.jpg)
Def Macros
![Page 14: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/14.jpg)
Def Macros
• Def macros replace well-typed terms with other well-typed terms
• Can contain arbitrary Scala constructs
![Page 15: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/15.jpg)
Practical exampleScala Logging
![Page 16: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/16.jpg)
Where?
logger.debug(s"Some $expensive message!")you call
if (logger.isDebugEnabled) logger.debug(s"Some $expensive message!")
replaced by macros
![Page 17: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/17.jpg)
M• Starts with the conditional keyword
• F
![Page 18: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/18.jpg)
I• Takes several parameter lists
![Page 19: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/19.jpg)
I• Takes several parameter lists
First comes a single parameter, of type
Context
![Page 20: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/20.jpg)
I• Takes several parameter lists
Next, followed by a list of parameters that have the same names as the macro
definition parameters
Macro definition
![Page 21: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/21.jpg)
I
• The original macro parameter has
• type T
• A macro implementation parameter has
• type c.Expr[T]
![Page 22: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/22.jpg)
Q• q"..." string interpolators that build code
• Unnecessary to directly implement the AST
• To use the quasiquotes from the macro, just write import c.universe._
![Page 23: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/23.jpg)
Q• Using the showRaw, it is possible to see the AST
![Page 24: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/24.jpg)
Goodness
• The hard to comprehend notion of meta programming
• def macros are similar to the concept of a typed method call
![Page 25: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/25.jpg)
Practical example
specs2
![Page 26: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/26.jpg)
Where?
• To use macros to automatically generate matchers for any complex typ
• MatcherMacros trait
![Page 27: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/27.jpg)
Where?
case class Cat(name: String, age: Int) !
// your test case val cat = Cat(name = "Tom", age = 2) cat must matchA[Cat].name("Tom")
you call
![Page 28: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/28.jpg)
Where?
def matchACat( name: Matcher[String] = AlwaysMatcher(), age: Matcher[Int] = AlwaysMatcher() ): Matcher[Cat] = name ^^ {(cat: Cat) => cat.name} and age ^^ {(cat: Cat) => cat.age}
replaced by macros
![Page 29: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/29.jpg)
M• Generics
• Has type parameters
![Page 30: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/30.jpg)
I• Type tags(actual type arguments)
along when the macro is expanded
![Page 31: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/31.jpg)
I• Type tags(actual type arguments)
along when the macro is expanded
Come with WeakTypeTag context bounds
![Page 32: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/32.jpg)
Goodness
• DRY approach
• Usage is fairly simple
![Page 33: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/33.jpg)
Blackbox vs Whitebox
• From 2.11
• Not implemented in 2.10 or in macro paradis
• in 2.12, only include blackbox macros
![Page 34: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/34.jpg)
Blackbox vs Whitebox
• 2.10
• scala.reflect.macros.Context
• 2.11
• scala.reflect.macros.blackbox.Context
• scala.reflect.macros.whitebox.Context
![Page 35: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/35.jpg)
Why distinction?• Type specification of macro is
class Foo class Bar extends Foo !
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) }
Scala 2.10
![Page 36: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/36.jpg)
Why distinction?
• Type checking during macro expansion
• Not affect after expansion
scala> FooExample.foo res0: Bar = Bar@4118f8dd
![Page 37: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/37.jpg)
B
• Type parameters of macro affect the type of after macro expansion
• When blackbox macro is used, Implicit Macros will not work
![Page 38: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/38.jpg)
No restrictions
• Same as 2.10 def macros
• Eshould be possible in 2.11
![Page 39: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/39.jpg)
Practical example
Spire
![Page 40: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/40.jpg)
Appendix
• A numeric library for Scala
• Using features such as macros, type classes
• Fast and Precise
![Page 41: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/41.jpg)
Where?
• Using string interpolation and macros, Spire provides convenient syntax for number types
• Evaluated at compile-time
![Page 42: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/42.jpg)
M• As usual
string interpolation
![Page 43: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/43.jpg)
I• Syntax check at compile-time
![Page 44: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/44.jpg)
I• Syntax check at compile-time
Occur at compile-time if any errors
encounter
![Page 45: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/45.jpg)
Goodness
• Static (compile-time) type check
• Runtime error decrease
![Page 46: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/46.jpg)
But, has weak side.
![Page 47: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/47.jpg)
Current state• Optimized towards compiler developers, not library
users
Complicated
![Page 48: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/48.jpg)
![Page 49: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/49.jpg)
Scala 2.12 (plan)
• Not introduce new features
• Bugfixes and stability improvements
• Mention later why reason
![Page 50: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/50.jpg)
scala.meta
• A new experimental API for metaprogramming
• F
![Page 51: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/51.jpg)
scala.meta
• Implemented in a library shipped separately from the official Scala distribution
• The first milestone release is scheduled for this fall
![Page 52: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/52.jpg)
The goal of scala.meta
• Metaprogramming easy
• New API is going to greatly simplify writing macros
• Not require knowledge of compiler internals
![Page 53: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/53.jpg)
Language model• All represented with trees
Types
NamesModifiers
…
Trees
scala.meta
Terms
Symbols
![Page 54: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/54.jpg)
Language model
• Keeps all the information about the program
• Comments also remain
• No information is lost anymore
![Page 55: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/55.jpg)
![Page 56: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/56.jpg)
Manner of utilization
• Code generation
• Static type checking
• etc…
![Page 57: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/57.jpg)
Summary
• Macros are actively used in the OSS library
• Can be more efficiently and safely programming
• Scala macros are evolving
![Page 58: What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!](https://reader038.vdocuments.net/reader038/viewer/2022102902/557d6a7bd8b42ac43c8b4c78/html5/thumbnails/58.jpg)
Thanks!!