Your SlideShare is downloading. ×
0
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Project kepler compile time metaprogramming for scala
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Project kepler compile time metaprogramming for scala

872

Published on

Scala Days, Project Kepler: Compile-Time Metaprogramming for Scala

Scala Days, Project Kepler: Compile-Time Metaprogramming for Scala

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
872
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Scala Macros Eugene BurmakoJan Christopher Vogt London, 2012
  • 2. Scala MacrosEmpower developers toextend the compiler and stay sane!This enables compile-time: – checks – processing – AST transformations – code generation – shipping of ASTs to runtime
  • 3. ImplementationCompile-time metaprogramming has longexisted in Lisp, so it should be easy toimplement, right?
  • 4. TrickinessHomoiconicity for a language with syntax andtypes is hard.Being non-hygienic is evil, being hygienicimposes a complexity tax on the spec.Need quasiquotations to make ASTmanipulations bearable. Yet another concept.
  • 5. BeautyMartin Odersky was suspicious:“And, wed need to be convinced that it isbeautifully simple, or it wont go into Scala”.Here is what convinced Martin:
  • 6. Cats
  • 7. The essenceScala reflection provides a slice of the compiler’scake. The infrastructure is already there.Macros are (Tree*, Type*) => Tree functions.Just as simple as that.Hygiene itself is implemented by a macro. Hencewe stay minimalistic and flexible.
  • 8. USE CASE: SLICK
  • 9. SLICK overviewval 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 SLICKval coffees = Queryable[Coffee]( /* db connection */ )coffees .filter( c => c.id == 101 ) implemented as a macro that works on argument AST at compile timedef 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 bodyobject 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 expansioncoffees .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 macroCome on, just kidding about the last one!
  • 17. Thank you!Questions and answerswww.scalamacros.org

×