SlideShare a Scribd company logo
Scala Macros

  Eugene Burmako
Jan Christopher Vogt



  London, 2012
Scala Macros
Empower developers to
extend the compiler and stay sane!

This enables compile-time:
  – checks
  – processing
  – AST transformations
  – code generation
  – shipping of ASTs to runtime
Implementation
Compile-time metaprogramming has long
existed in Lisp, so it should be easy to
implement, right?
Trickiness
Homoiconicity for a language with syntax and
types is hard.

Being non-hygienic is evil, being hygienic
imposes a complexity tax on the spec.

Need quasiquotations to make AST
manipulations bearable. Yet another concept.
Beauty
Martin Odersky was suspicious:

“And, we'd need to be convinced that it is
beautifully simple, or it won't go into Scala”.

Here is what convinced Martin:
Cats
The essence
Scala reflection provides a slice of the compiler’s
cake. The infrastructure is already there.

Macros are (Tree*, Type*) => Tree functions.
Just as simple as that.

Hygiene itself is implemented by a macro. Hence
we stay minimalistic and flexible.
USE CASE: SLICK
SLICK overview
val coffees = Queryable[Coffee]( /* db connection */ )
coffees      .filter( c => c.id == 101 )

implemented using macros

                handed over
                to runtime
   compile time (reification)      runtime
                                                    SQL

     SLICK
                           Scala     SLICK Query
   Queryable                                       NoSQL
                           ASTs          ASTs
   frontend
                                                     Web
                                                   services


                                                              9
Macros in SLICK
val coffees = Queryable[Coffee]( /* db connection */ )
coffees      .filter( c => c.id == 101 )


    implemented as a macro that works on argument AST at compile time

def filter[T]( predicate: T => Boolean ) : Queryable[T] =
                                 macro QueryableMacros.filter[T]

object QueryableMacros{
  def filter[T:c.TypeTag] (c: scala.reflect.makro.Context)
                 (predicate : c.mirror.Expr[T => Boolean]) = …
}
A macro body
object QueryableMacros{

  // macro implementation signature
  def filter[T:c.TypeTag] (c: scala.reflect.makro.Context)
                 (predicate: c.mirror.Expr[T => Boolean]) = {

  // reify tree (ship to runtime)

  val reifiedPredicate =
      c.mirror.Expr[ reflect.mirror.Expr[T => Boolean] ](
        c.reifyTree( c.reflectMirrorPrefix, projection.tree ) )
                                                   splicing
   // splice into runtime call
  c.reify{ translate("filter", c.prefix.eval, reifiedPredicate.eval ) }
}}
                          inlined at the call site (macro expansion),
                          translates queries further at runtime, e.g. to SQL
Resulting macro expansion

coffees      .filter( c => c.id == 101 )




translate(
  "filter",
  coffees,
  Function( List("c"),                      Scala AST
      Apply( Select("c", "id"), "==", List(
        Literal(Constant(101)))))
)
                           That are macros in SLICK
Applications in the compiler
• Removed a compiler phase (LiftCode)
• Made a 80% solution (manifests) a 99%
  solution (type tags)
• Manifests à la carte – no longer hardcoded in
  Implicits.scala, it‘s just several macros (you
  can write your own implementations!)
• Ideas: SourceLocations, SourceContexts, static
  requirements on the compiler and the
  compilation environment
Applications in the wild
•   SLICK
•   Macrocosm
•   Scalatex
•   Expecty
•   Scalaxy
•   Ideas: procedure typing with arrows, lens
    generation, ACP DSL, zero-overhead mocks
Future work
•   Debugging generated code
•   Untyped macros
•   Type macros
•   Macro annotations
•   Replacing the compiler with a macro
Future work
•   Debugging generated code
•   Untyped macros
•   Type macros
•   Macro annotations
•   Replacing the compiler with a macro

Come on, just kidding about the last one!
Thank you!



Questions and answers
www.scalamacros.org

More Related Content

PPTX
Scalable Applications with Scala
PDF
From Zero to Stream Processing
PDF
Intro to RxJava/RxAndroid - GDG Munich Android
PDF
从 Oracle 合并到 my sql npr 实例分析
PPTX
RxJava Applied
PDF
What's new in Scala and the Scala IDE for Eclipse for 2.8.0
PDF
RxJava applied [JavaDay Kyiv 2016]
PPTX
Scala.js for large and complex frontend apps
Scalable Applications with Scala
From Zero to Stream Processing
Intro to RxJava/RxAndroid - GDG Munich Android
从 Oracle 合并到 my sql npr 实例分析
RxJava Applied
What's new in Scala and the Scala IDE for Eclipse for 2.8.0
RxJava applied [JavaDay Kyiv 2016]
Scala.js for large and complex frontend apps

What's hot (20)

KEY
The Why and How of Scala at Twitter
PDF
A Brief, but Dense, Intro to Scala
PDF
Migrating Legacy Data (Ruby Midwest)
PPTX
Introduction to rx java for android
PDF
RxJava on Android
PDF
Practical RxJava for Android
PDF
Akka lsug skills matter
PDF
Streams, Streams Everywhere! An Introduction to Rx
PPTX
An Introduction to RxJava
PDF
Reenabling SOAP using ERJaxWS
KEY
Rails and Legacy Databases - RailsConf 2009
PDF
Reaching the lambda heaven
PDF
Clojure & Scala
PPTX
Fundamentals of java --- version 2
PDF
Reactive programming on Android
PPTX
From Ruby to Scala
PDF
Reactive database access with Slick3
PDF
Reactive Android: RxJava and beyond
PDF
How we use Fluentd in Treasure Data
PDF
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
The Why and How of Scala at Twitter
A Brief, but Dense, Intro to Scala
Migrating Legacy Data (Ruby Midwest)
Introduction to rx java for android
RxJava on Android
Practical RxJava for Android
Akka lsug skills matter
Streams, Streams Everywhere! An Introduction to Rx
An Introduction to RxJava
Reenabling SOAP using ERJaxWS
Rails and Legacy Databases - RailsConf 2009
Reaching the lambda heaven
Clojure & Scala
Fundamentals of java --- version 2
Reactive programming on Android
From Ruby to Scala
Reactive database access with Slick3
Reactive Android: RxJava and beyond
How we use Fluentd in Treasure Data
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
Ad

Viewers also liked (11)

PPT
PDF
Arvindsujeeth scaladays12
PPT
Man made marvels
PDF
Scala days mizushima
PDF
Prediction suretogowrong
PDF
Test driven infrastructure
PDF
Cnc scala-presentation
DOC
Proposal parade seni
PPTX
Frase dan klausa
PPTX
Zaharia spark-scala-days-2012
PPTX
EDS selection & implementation @ CCC
Arvindsujeeth scaladays12
Man made marvels
Scala days mizushima
Prediction suretogowrong
Test driven infrastructure
Cnc scala-presentation
Proposal parade seni
Frase dan klausa
Zaharia spark-scala-days-2012
EDS selection & implementation @ CCC
Ad

Similar to Project kepler compile time metaprogramming for scala (20)

PDF
Martin Odersky: What's next for Scala
PDF
Cliff Moon - Building Polyglot Distributed Systems with Scalang, Boundary Tec...
PDF
Scala In The Wild
PDF
Macros and reflection in scala 2.10
PDF
An Introduction to Scala - Blending OO and Functional Paradigms
PDF
Pune Clojure Course Outline
PDF
An Introduction to Scala for Java Developers
PDF
BCS SPA 2010 - An Introduction to Scala for Java Developers
PDF
Miles Sabin Introduction To Scala For Java Developers
PDF
A Brief Introduction to Scala for Java Developers
PDF
Eugene Burmako
PDF
Using Scala for building DSLs
PDF
Scala at GenevaJUG by Iulian Dragos
PDF
scalaliftoff2009.pdf
PDF
scalaliftoff2009.pdf
PDF
scalaliftoff2009.pdf
PDF
scalaliftoff2009.pdf
PPTX
Introduction to Clojure and why it's hot for Sart-Ups
PDF
What's a macro?: Learning by Examples
PDF
Learning Functional Programming Without Growing a Neckbeard
Martin Odersky: What's next for Scala
Cliff Moon - Building Polyglot Distributed Systems with Scalang, Boundary Tec...
Scala In The Wild
Macros and reflection in scala 2.10
An Introduction to Scala - Blending OO and Functional Paradigms
Pune Clojure Course Outline
An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
Miles Sabin Introduction To Scala For Java Developers
A Brief Introduction to Scala for Java Developers
Eugene Burmako
Using Scala for building DSLs
Scala at GenevaJUG by Iulian Dragos
scalaliftoff2009.pdf
scalaliftoff2009.pdf
scalaliftoff2009.pdf
scalaliftoff2009.pdf
Introduction to Clojure and why it's hot for Sart-Ups
What's a macro?: Learning by Examples
Learning Functional Programming Without Growing a Neckbeard

More from Skills Matter Talks (9)

PDF
Couch db skillsmatter-prognosql
PDF
Jordan west real workscalazfinal2
PDF
(Oleg zhurakousky)spring integration-scala-intro
PPT
SCALA DAYS 2012: Ben Parker on Interactivity - Anti-XML in Anger
PDF
Real World Scalaz
PDF
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
PDF
Tmt predictions 2011
PDF
Martin sustrik future_of_messaging
PDF
Marek pubsubhuddle realtime_web
Couch db skillsmatter-prognosql
Jordan west real workscalazfinal2
(Oleg zhurakousky)spring integration-scala-intro
SCALA DAYS 2012: Ben Parker on Interactivity - Anti-XML in Anger
Real World Scalaz
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
Tmt predictions 2011
Martin sustrik future_of_messaging
Marek pubsubhuddle realtime_web

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
cuic standard and advanced reporting.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Modernizing your data center with Dell and AMD
PDF
Empathic Computing: Creating Shared Understanding
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Unlocking AI with Model Context Protocol (MCP)
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
Chapter 3 Spatial Domain Image Processing.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Modernizing your data center with Dell and AMD
Empathic Computing: Creating Shared Understanding
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.

Project kepler compile time metaprogramming for scala

  • 1. Scala Macros Eugene Burmako Jan Christopher Vogt London, 2012
  • 2. Scala Macros Empower developers to extend the compiler and stay sane! This enables compile-time: – checks – processing – AST transformations – code generation – shipping of ASTs to runtime
  • 3. Implementation Compile-time metaprogramming has long existed in Lisp, so it should be easy to implement, right?
  • 4. Trickiness Homoiconicity for a language with syntax and types is hard. Being non-hygienic is evil, being hygienic imposes a complexity tax on the spec. Need quasiquotations to make AST manipulations bearable. Yet another concept.
  • 5. Beauty Martin Odersky was suspicious: “And, we'd need to be convinced that it is beautifully simple, or it won't go into Scala”. Here is what convinced Martin:
  • 7. The essence Scala reflection provides a slice of the compiler’s cake. The infrastructure is already there. Macros are (Tree*, Type*) => Tree functions. Just as simple as that. Hygiene itself is implemented by a macro. Hence we stay minimalistic and flexible.
  • 9. SLICK overview val coffees = Queryable[Coffee]( /* db connection */ ) coffees .filter( c => c.id == 101 ) implemented using macros handed over to runtime compile time (reification) runtime SQL SLICK Scala SLICK Query Queryable NoSQL ASTs ASTs frontend Web services 9
  • 10. Macros in SLICK val coffees = Queryable[Coffee]( /* db connection */ ) coffees .filter( c => c.id == 101 ) implemented as a macro that works on argument AST at compile time def filter[T]( predicate: T => Boolean ) : Queryable[T] = macro QueryableMacros.filter[T] object QueryableMacros{ def filter[T:c.TypeTag] (c: scala.reflect.makro.Context) (predicate : c.mirror.Expr[T => Boolean]) = … }
  • 11. A macro body object QueryableMacros{ // macro implementation signature def filter[T:c.TypeTag] (c: scala.reflect.makro.Context) (predicate: c.mirror.Expr[T => Boolean]) = { // reify tree (ship to runtime) val reifiedPredicate = c.mirror.Expr[ reflect.mirror.Expr[T => Boolean] ]( c.reifyTree( c.reflectMirrorPrefix, projection.tree ) ) splicing // splice into runtime call c.reify{ translate("filter", c.prefix.eval, reifiedPredicate.eval ) } }} inlined at the call site (macro expansion), translates queries further at runtime, e.g. to SQL
  • 12. Resulting macro expansion coffees .filter( c => c.id == 101 ) translate( "filter", coffees, Function( List("c"), Scala AST Apply( Select("c", "id"), "==", List( Literal(Constant(101))))) ) That are macros in SLICK
  • 13. Applications in the compiler • Removed a compiler phase (LiftCode) • Made a 80% solution (manifests) a 99% solution (type tags) • Manifests à la carte – no longer hardcoded in Implicits.scala, it‘s just several macros (you can write your own implementations!) • Ideas: SourceLocations, SourceContexts, static requirements on the compiler and the compilation environment
  • 14. Applications in the wild • SLICK • Macrocosm • Scalatex • Expecty • Scalaxy • Ideas: procedure typing with arrows, lens generation, ACP DSL, zero-overhead mocks
  • 15. Future work • Debugging generated code • Untyped macros • Type macros • Macro annotations • Replacing the compiler with a macro
  • 16. Future work • Debugging generated code • Untyped macros • Type macros • Macro annotations • Replacing the compiler with a macro Come on, just kidding about the last one!
  • 17. Thank you! Questions and answers www.scalamacros.org