state of the meta, spring 2015downloads.typesafe.com/website/presentations/... · part 2: quick...
TRANSCRIPT
![Page 1: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/1.jpg)
State of the Meta, Spring 2015
Eugene Burmako (@xeno by)
Ecole Polytechnique Federale de Lausannehttp://scalameta.org/
17 March 2015
![Page 2: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/2.jpg)
scala.meta
The goal of scala.meta is to make metaprogramming easy
— scalameta.org
2
![Page 3: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/3.jpg)
scala.meta
The goal of scala.meta is to make metaprogramming easy,ensuring that it:1) Doesn’t require knowledge of compiler internals
— scalameta.org
3
![Page 4: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/4.jpg)
scala.meta
The goal of scala.meta is to make metaprogramming easy,ensuring that it:1) Doesn’t require knowledge of compiler internals2) Is safe from compiler crashes by construction
— scalameta.org
4
![Page 5: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/5.jpg)
scala.meta
The goal of scala.meta is to make metaprogramming easy,ensuring that it:1) Doesn’t require knowledge of compiler internals2) Is safe from compiler crashes by construction3) Is portable across a wide range of implementors
— scalameta.org
5
![Page 6: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/6.jpg)
ScalaDays Berlin
6
![Page 7: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/7.jpg)
ScalaDays San Francisco
7
![Page 8: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/8.jpg)
Outline
▶ The next-generation metaprogramming ecosystem
▶ Quick introduction into scala.meta
▶ Key concepts of the syntactic API
▶ Key concepts of the semantic API
8
![Page 9: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/9.jpg)
Credits
Big thanks to everyone who helped turning scala.meta into reality!
▶ Uladzimir Abramchuk
▶ Eric Beguet
▶ Igor Bogomolov
▶ Eugene Burmako
▶ Mathieu Demarne
▶ Martin Duhem
▶ Adrien Ghosn
▶ Vojin Jovanovic
▶ Guillaume Masse
▶ Mikhail Mutcianko
▶ Dmitry Naydanov
▶ Artem Nikiforov
▶ Vladimir Nikolaev
▶ Martin Odersky
▶ Alexander Podkhalyuzin
▶ Jatin Puri
▶ Dmitry Petrashko
▶ Denys Shabalin
9
![Page 10: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/10.jpg)
Part 1: The next-generation metaprogramming ecosystem
![Page 11: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/11.jpg)
High-level view
▶ scala.reflect has known issues, but it works and is well-understood
▶ What does scala.meta have to offer, concretely?
▶ Here’s our proposed design of the future metaprogramming ecosystem
▶ There’s work being done towards implementing it as we speak
11
![Page 12: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/12.jpg)
Architecture in a nutshell
▶ Ecosystem of platform-independent metaprogramsmacros, style checkers, code formatters, refactorings,... (other tools, utilities, libraries)
▶ Library of platform-independent metaprogramming APIshttps://github.com/scalameta/scalameta
▶ Collection of platform-dependent implementationshttps://github.com/scalameta/scalahost,... (intellij, dotty, etc)
12
![Page 13: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/13.jpg)
Architecture in a nutshell
▶ Ecosystem of platform-independent metaprogramsmacros, style checkers, code formatters, refactorings,... (other tools, utilities, libraries)
▶ Library of platform-independent metaprogramming APIshttps://github.com/scalameta/scalameta
▶ Collection of platform-dependent implementationshttps://github.com/scalameta/scalahost,... (intellij, dotty, etc)
13
![Page 14: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/14.jpg)
Architecture in a nutshell
▶ Ecosystem of platform-independent metaprogramsmacros, style checkers, code formatters, refactorings,... (other tools, utilities, libraries)
▶ Library of platform-independent metaprogramming APIshttps://github.com/scalameta/scalameta
▶ Collection of platform-dependent implementationshttps://github.com/scalameta/scalahost,... (intellij, dotty, etc)
14
![Page 15: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/15.jpg)
Architecture in more detail
scala.meta
TASTY binaries
15
![Page 16: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/16.jpg)
The frontend/backend separation
scala.meta
frontend
backend
TASTY binaries
16
![Page 17: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/17.jpg)
Notable frontend metaprograms
scalac
Intellij macros
tools
scala.meta
frontend
backend
TASTY binaries
17
![Page 18: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/18.jpg)
How do metaprograms read TASTY? (Design #1)
scalac
Intellij macros
tools
scala.meta
frontend
backend
TASTY binaries
18
![Page 19: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/19.jpg)
How do metaprograms read TASTY? (Design #2)
scalac
Intellij macros
tools
scala.meta
frontend
backend
TASTY binaries
19
![Page 20: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/20.jpg)
What about Dotty?
scalac
Intellij macros
tools
scala.meta
frontenddotc
backend
TASTY binaries
20
![Page 21: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/21.jpg)
Revisiting tools (Design #1)
scalac
Intellij macros
tools
scala.meta
frontenddotc
backend
TASTY binaries
21
![Page 22: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/22.jpg)
Revisiting tools (Design #2)
scalac
Intellij macros
tools
scala.meta
frontenddotc
backend
TASTY binaries
22
![Page 23: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/23.jpg)
Summary
scalac
Intellij macros
tools
scala.meta
frontenddotc
backend
TASTY binaries
23
![Page 24: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/24.jpg)
Summary
scalac
Intellij macros
tools
scala.meta
frontenddotc
backend
TASTY binaries
scalameta/scalameta (platform-independent)
24
![Page 25: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/25.jpg)
Summary
scalac
Intellij macros
tools
scala.meta
frontenddotc
backend
TASTY binaries
scalameta/scalameta (platform-independent)
organization/platformhost (platform-dependent)
25
![Page 26: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/26.jpg)
Summary
scalac
Intellij macros
tools
scala.meta
frontenddotc
backend
TASTY binaries
scalameta/scalameta (platform-independent)
organization/platformhost (platform-dependent)
your/project (platform-independent)
26
![Page 27: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/27.jpg)
Part 2: Quick introduction into scala.meta
![Page 28: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/28.jpg)
Getting all definitions in a source file
28
![Page 29: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/29.jpg)
Getting all definitions in a source file
import scala.tools.meta.Metaprogram
import scala.tools.meta.Settings
class Analysis(val settings: Settings) extends Metaprogram {
}
val metaprogram = new Analysis(new Settings())
29
![Page 30: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/30.jpg)
Getting all definitions in a source file
import scala.tools.meta.Metaprogram
import scala.tools.meta.Settings
import scala.meta.internal.util.SourceFile
class Analysis(val settings: Settings) extends Metaprogram {
}
val metaprogram = new Analysis(new Settings())
val sourceFile = new SourceFile(new java.io.File(args(1)))
30
![Page 31: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/31.jpg)
Getting all definitions in a source file
import scala.tools.meta.Metaprogram
import scala.tools.meta.Settings
import scala.meta.internal.util.SourceFile
class Analysis(val settings: Settings) extends Metaprogram {
def apply(sourceFile: SourceFile) = {
import multiverse._
val unit = newCompilationUnit(sourceFile)
val parser = newUnitParser(unit)
}
}
val metaprogram = new Analysis(new Settings())
val sourceFile = new SourceFile(new java.io.File(args(1)))
metaprogram.apply(sourceFile)
31
![Page 32: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/32.jpg)
![Page 33: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/33.jpg)
On a serious note
▶ Just import scala.meta.
▶ +0-2 additional lines of code depending on the context
33
![Page 34: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/34.jpg)
Syntactic APIs
import scala.meta._
import scala.meta.dialects.Scala211
▶ Tokenization
▶ Parsing
▶ Positions
▶ Quasiquotes
34
![Page 35: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/35.jpg)
Semantic APIs
import scala.meta._
implicit val c: scala.meta.semantic.Context = ...
▶ Name resolution
▶ Typechecking
▶ Members
▶ Subtyping
▶ ...
35
![Page 36: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/36.jpg)
What’s a context?
trait Context {
def dialect: Dialect
def tpe(term: Term): Type
def tpe(param: Term.Param): Type.Arg
def defns(ref: Ref): Seq[Member]
def members(tpe: Type): Seq[Member]
def isSubType(tpe1: Type, tpe2: Type): Boolean
def lub(tpes: Seq[Type]): Type
... // 7 more methods
}
36
![Page 37: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/37.jpg)
Where do contexts come from?
▶ Scalac: https://github.com/scalameta/scalahost
▶ Intellij: in the works
▶ Dotty: planned
▶ Anywhere else: anyone can implement a context
37
![Page 38: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/38.jpg)
Part 3: Key concepts of the syntactic API
![Page 39: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/39.jpg)
Getting started
$ scala
scala> import scala.meta._
import scala.meta._
scala> import scala.meta.dialects.Scala211
import scala.meta.dialects.Scala211
39
![Page 40: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/40.jpg)
Design goals (ScalaDays Berlin)
▶ In scala.meta, we keep all syntactic information about the program
▶ Nothing is desugared (e.g. for loops or string interpolations)
▶ Nothing is thrown away (e.g. comments or formatting details)
40
![Page 41: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/41.jpg)
Implementation vehicle
First-class tokens
41
![Page 42: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/42.jpg)
Tokens
scala> "class C { def x = 2 }".tokens
...
res0: Vector[meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6), (7..7), { (8..8), (9..9),
def (10..12), (13..13), x (14..14), (15..15), = (16..16),
(17..17), 2 (18..18), (19..19), } (20..20), EOF (21..20))
42
![Page 43: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/43.jpg)
Tokens
scala> "class C { def x = 2 }".tokens
res0: Vector[meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6), (7..7), { (8..8), (9..9),
def (10..12), (13..13), x (14..14), (15..15), = (16..16),
(17..17), 2 (18..18), (19..19), } (20..20), EOF (21..20))
43
![Page 44: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/44.jpg)
Tokens
scala> "class C { def x = 2 }".tokens
res0: Vector[meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6), (7..7), { (8..8), (9..9),
def (10..12), (13..13), x (14..14), (15..15), = (16..16),
(17..17), 2 (18..18), (19..19), } (20..20), EOF (21..20))
44
![Page 45: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/45.jpg)
Tokens
scala> "class C { def x = 2 }".tokens
res0: Vector[meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6), (7..7), { (8..8), (9..9),
def (10..12), (13..13), x (14..14), (15..15), = (16..16),
(17..17), 2 (18..18), (19..19), } (20..20), EOF (21..20))
45
![Page 46: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/46.jpg)
Tokens
scala> "class C { def x = 2 }".tokens
res0: Vector[meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6), (7..7), { (8..8), (9..9),
def (10..12), (13..13), x (14..14), (15..15), = (16..16),
(17..17), 2 (18..18), (19..19), } (20..20), EOF (21..20))
46
![Page 47: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/47.jpg)
Tokens
scala> "class C { def x = 2 }".tokens
res0: Vector[meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6), (7..7), { (8..8), (9..9),
def (10..12), (13..13), x (14..14), (15..15), = (16..16),
(17..17), 2 (18..18), (19..19), } (20..20), EOF (21..20))
47
![Page 48: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/48.jpg)
High-fidelity parsers
scala> "class C".parse[Stat]
res1: scala.meta.Stat = class C
scala> "class C {}".parse[Stat]
res2: scala.meta.Stat = class C {}
48
![Page 49: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/49.jpg)
High-fidelity parsers
scala> "class C".parse[Stat]
res1: scala.meta.Stat = class C
scala> "class C {}".parse[Stat]
res2: scala.meta.Stat = class C {}
scala> res1.tokens
res3: Seq[scala.meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6), EOF(7..6))
scala> res2.tokens
res4: Seq[scala.meta.Token] = Vector(BOF (0..-1),
class (0..4), (5..5), C (6..6),
(7..7), { (8..8), } (9..9), EOF (10..9))
49
![Page 50: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/50.jpg)
Automatic and precise range positions
scala> "class C { def x = 2 }".parse[Stat]
res5: scala.meta.Stat = class C
scala> val q"class C { $method }" = res5
method: scala.meta.Stat = def x = 2
50
![Page 51: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/51.jpg)
Automatic and precise range positions
scala> "class C { def x = 2 }".parse[Stat]
res5: scala.meta.Stat = class C
scala> val q"class C { $method }" = res5
method: scala.meta.Stat = def x = 2
scala> method.tokens
res6: Seq[scala.meta.Token] = Vector(
def (10..12), (13..13), x (14..14), (15..15),
= (16..16), (17..17), 2 (18..18))
51
![Page 52: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/52.jpg)
Hacky quasiquotes in scala.reflect
$ scala -Yquasiquote-debug
scala> import scala.reflect.runtime.universe.
import scala.reflect.runtime.universe.
scala> val name = TypeName("C")
name: reflect.runtime.universe.TypeName = C
scala> q"class $name"
...
52
![Page 53: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/53.jpg)
Hacky quasiquotes in scala.reflect
$ scala -Yquasiquote-debug
scala> import scala.reflect.runtime.universe.
import scala.reflect.runtime.universe.
scala> val name = TypeName("C")
name: reflect.runtime.universe.TypeName = C
scala> q"class $name"
...
code to parse:
class qq$a2912896$macro$1
parsed:
Block(List(), ClassDef(Modifiers(),
TypeName("qq$a2912896$macro$1"), List(), Template(...))
...
53
![Page 54: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/54.jpg)
Principled quasiquotes in scala.meta
$ scala -Dquasiquote.debug
scala> import scala.meta. , dialects.Scala211
import scala.meta.
import dialects.Scala211
scala> val name = t"C"
name: scala.meta.Type.Name = C
scala> q"class $name"
...
54
![Page 55: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/55.jpg)
Principled quasiquotes in scala.meta
$ scala -Dquasiquote.debug
scala> import scala.meta. , dialects.Scala211
import scala.meta.
import dialects.Scala211
scala> val name = t"C"
name: scala.meta.Type.Name = C
scala> q"class $name"
...
tokens: Vector(BOF (0..-1), class (0..4), (5..5),
$name (6..10), EOF (11..10))
...
55
![Page 56: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/56.jpg)
Derived technologies
First-class tokens enable:
▶ High-fidelity parsers
▶ Automatic and precise range positions
▶ Principled quasiquotes
56
![Page 57: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/57.jpg)
Part 4: Key concepts of the semantic API
![Page 58: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/58.jpg)
Getting started
$ scala
scala> import scala.meta.
import scala.meta.
scala> implicit val c = Scalahost.mkStandaloneContext()
c: scala.meta.macros.Context = ...
▶ Scalahost.mkStandaloneContext(”-cp ...”): useful for experimentation
▶ Scalahost.mkGlobalContext(global): useful for compiler plugins
▶ Implicit scope when writing macros: very convenient!
▶ Anywhere else: anyone can implement a context
58
![Page 59: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/59.jpg)
Getting started
$ scala
scala> import scala.meta.
import scala.meta.
scala> implicit val c = Scalahost.mkStandaloneContext()
c: scala.meta.macros.Context = ...
Contexts can come from:
▶ Scalahost.mkStandaloneContext(”-cp ...”): useful for experimentation
▶ Scalahost.mkGlobalContext(global): useful for compiler plugins
▶ Implicit scope when writing macros: very convenient!
▶ Anywhere else: anyone can implement a context
59
![Page 60: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/60.jpg)
Getting started
$ scala
scala> import scala.meta.
import scala.meta.
scala> implicit val c = Scalahost.mkStandaloneContext()
c: scala.meta.macros.Context = ...
Contexts can come from:
▶ Scalahost.mkStandaloneContext(”-cp ...”): useful for experimentation
▶ Scalahost.mkGlobalContext(global): useful for compiler plugins
▶ Implicit scope when writing macros: very convenient!
▶ Anywhere else: anyone can implement a context
60
![Page 61: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/61.jpg)
Getting started
$ scala
scala> import scala.meta.
import scala.meta.
scala> implicit val c = Scalahost.mkStandaloneContext()
c: scala.meta.macros.Context = ...
Contexts can come from:
▶ Scalahost.mkStandaloneContext(”-cp ...”): useful for experimentation
▶ Scalahost.mkGlobalContext(global): useful for compiler plugins
▶ Implicit scope when writing macros: very convenient!
▶ Anywhere else: anyone can implement a context
61
![Page 62: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/62.jpg)
Getting started
$ scala
scala> import scala.meta.
import scala.meta.
scala> implicit val c = Scalahost.mkStandaloneContext()
c: scala.meta.macros.Context = ...
Contexts can come from:
▶ Scalahost.mkStandaloneContext(”-cp ...”): useful for experimentation
▶ Scalahost.mkGlobalContext(global): useful for compiler plugins
▶ Implicit scope when writing macros: very convenient!
▶ Anywhere else: anyone can implement a context
62
![Page 63: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/63.jpg)
Design goals (ScalaDays Berlin)
▶ In scala.meta, we model everything just with its abstract syntax
▶ Types, members, names, modifiers: all represented with trees
▶ There’s only one data structure, so there’s only one way to do it
63
![Page 64: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/64.jpg)
Implementation vehicle
First-class names
64
![Page 65: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/65.jpg)
Bindings in scala.reflect
$ scala
scala> import scala.reflect.runtime.universe.
import scala.reflect.runtime.universe.
scala> showRaw(q"class C { def x = 2; def y = x }")
...
65
![Page 66: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/66.jpg)
Bindings in scala.reflect
$ scala
scala> import scala.reflect.runtime.universe.
import scala.reflect.runtime.universe.
scala> showRaw(q"class C { def x = 2; def y = x }")
res1: String = ClassDef(
Modifiers(), TypeName("C"), List(),
Template(
List(Select(Ident(scala), TypeName("AnyRef"))),
noSelfType,
List(
DefDef(NoMods, termNames.CONSTRUCTOR, ...),
DefDef(NoMods, TermName("x"), ..., Literal(Constant(2))),
DefDef(NoMods, TermName("y"), ..., Ident(TermName("x"))))))
66
![Page 67: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/67.jpg)
Bindings in scala.reflect
$ scala
scala> import scala.reflect.runtime.universe.
import scala.reflect.runtime.universe.
scala> showRaw(q"class C { def x = 2; def y = x }")
res1: String = ClassDef(
Modifiers(), TypeName("C"), List(),
Template(
List(Select(Ident(scala), TypeName("AnyRef"))),
noSelfType,
List(
DefDef(NoMods, termNames.CONSTRUCTOR, ...),
DefDef(NoMods, TermName("x"), ..., Literal(Constant(2))),
DefDef(NoMods, TermName("y"), ..., Ident(TermName("x"))))))
67
![Page 68: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/68.jpg)
Bindings in scala.reflect
$ scala
scala> import scala.reflect.runtime.universe.
import scala.reflect.runtime.universe.
scala> showRaw(q"class C { def x = 2; def y = x }")
res1: String = ClassDef(
Modifiers(), "C", List(),
Template(
List(Select(Ident(scala), "AnyRef")),
noSelfType,
List(
DefDef(NoMods, termNames.CONSTRUCTOR, ...),
DefDef(NoMods, "x", ..., Literal(Constant(2))),
DefDef(NoMods, "y", ..., Ident("x")))))
68
![Page 69: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/69.jpg)
Bindings in scala.reflect
$ scala
scala> import scala.reflect.runtime.universe.
import scala.reflect.runtime.universe.
scala> showRaw(q"class C { def x = 2; def y = x }")
res1: String = ClassDef(
Modifiers(), "C", List(),
Template(
List(Select(Ident(scala), "AnyRef")),
noSelfType,
List(
DefDef(NoMods, termNames.CONSTRUCTOR, ...),
DefDef(NoMods, "x", ..., Literal(Constant(2))),
DefDef(NoMods, "y", ..., Ident("x")))))
69
![Page 70: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/70.jpg)
Bindings in scala.meta
$ scala
scala> import scala.meta. , dialects.Scala211
import scala.meta.
import dialects.Scala211
scala> q"class C { def x = 2; def y = x }".show[Raw]
res0: String = Defn.Class(
Nil, Type.Name("C"), Nil,
Ctor.Primary(Nil, Ctor.Name("this"), Nil),
Template(
Nil, Nil,
Term.Param(Nil, Name.Anonymous(), None, None),
Some(List(
Defn.Def(Nil, Term.Name("x"), ..., Lit.Int(2)),
Defn.Def(Nil, Term.Name("y"), ..., Term.Name("x"))))))
70
![Page 71: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/71.jpg)
Cute trees
71
![Page 72: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/72.jpg)
Scary trees
72
![Page 73: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/73.jpg)
Key example
List[Int]
73
![Page 74: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/74.jpg)
Key example
scala> t"List[Int]".show[Raw]
res1: String =
Type.Apply(Type.Name("List"), List(Type.Name("Int")))
74
![Page 75: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/75.jpg)
Key example
scala> t"List[Int]".show[Raw]
res1: String =
Type.Apply(Type.Name("List"), List(Type.Name("Int")))
scala> t"List[Int]".show[Semantics]
res2: String =
Type.Apply(Type.Name("List")[1], List(Type.Name("Int")[2]))
[1] Type.Singleton(Term.Name("package")[4])::scala.package#List
[2] Type.Singleton(Term.Name("scala")[3])::scala#Int
...
75
![Page 76: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/76.jpg)
Name resolution
scala> implicit val c = Scalahost.mkStandaloneContext()
c: scala.meta.macros.Context = ...
scala> q"scala.collection.immutable.List".defn
res3: scala.meta.Member.Term = object List extends
SeqFactory[List] with Serializable { ... }
scala> res3.name
res4: scala.meta.Term.Name = List
76
![Page 77: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/77.jpg)
Other semantic APIs
scala> q"scala.collection.immutable.List".defs("apply")
res5: scala.meta.Member.Term =
override def apply[A](xs: A*): List[A] = ???
scala> q"scala.collection.immutable.List".parents
res6: Seq[scala.meta.Member.Term] =
List(abstract class SeqFactory...)
77
![Page 78: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/78.jpg)
Derived technologies
First-class names enable:
▶ Unification of trees, types and symbols
▶ Referential transparency and hygiene (under development!)
▶ Simpler mental model of metaprogramming
78
![Page 79: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/79.jpg)
Wrapping up
![Page 80: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/80.jpg)
Summary
▶ scala.meta is a one-stop solution to frontend metaprogramming
▶ Our key innovations include first-class support for tokens and names
▶ This gives rise to a powerful, yet simple model of Scala programs
▶ We are working hard to provide an alpha release soon
Upcoming presentations:Live demo at a Scala Bay meetup in Mountain View (19 March)Hands-on workshop at flatMap in Oslo (27-28 April)Next status update at ScalaDays in Amsterdam (8-10 June)
80
![Page 81: State of the Meta, Spring 2015downloads.typesafe.com/website/presentations/... · Part 2: Quick introduction into scala.meta. Getting all de nitions in a source le 28. Getting all](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc4b2fb9fc342494627b03f/html5/thumbnails/81.jpg)
Summary
▶ scala.meta is a one-stop solution to frontend metaprogramming
▶ Our key innovations include first-class support for tokens and names
▶ This gives rise to a powerful, yet simple model of Scala programs
▶ We are working hard to provide an alpha release soon
Upcoming presentations:Live demo at a Scala Bay meetup in Mountain View (19 March)Hands-on workshop at flatMap in Oslo (27-28 April)Next status update at ScalaDays in Amsterdam (8-10 June)
81