Macros and reflection in scala 2.10


Published on

My Topconf Tallinn 2012 talk on macros and reflection in Scala 2.10.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Macros and reflection in scala 2.10

  1. 1. Macros &reflection in Scala 2.10
  2. 2. About meConsultantJVM/Java - 7 years @apnylleScala - 1 year
  3. 3. IMPORTANTWARNING The reflection/macros in Scala 2.10 are experimental and pretty complex, you might shoot yourself in the foot or worse and be the first one to do it. (They are also totally awesome)
  4. 4. Photo: Christian HolmérWhat is reflection? ?
  5. 5. What is reflection? wikipedia ”... reflection is the ability of a computer program to examine ... and modify the structure and behavior ... of an object at runtime.
  6. 6. But ...Hasn’t Scala already gotthe Java reflection API?!No support for Scala constructs!Type erasure List[Int] + compiler = List :(
  7. 7. we want something better!Also:Homoiconic(ness) - express the language in itselfOne API for reflection, macros (and thecompiler)Support for reificationNo overhead when not used
  8. 8. Talking is cheap...Let’s Look AtSome Code
  9. 9. What about macros?wikipedia”A macro (from the Greek μακρό for "big" or "far")in computer science is a rule or pattern thatspecifies how a certain input sequence (often asequence of characters) should be mapped to areplacement input sequence (also often a sequenceof characters) according to a defined procedure. Wat!?!
  10. 10. Ok, Iwhat is ait really? but Get macro, A regular scala function AST => AST Called by the compiler
  11. 11. Ok,soIwhat is an AST, really? Get it if(1 == ”1”) { true } else { false } if(1.==(”1”)) { true } else { false }if(1.$eq$eq(”1”)) { true } else { false }
  12. 12. if(1.$eq$eq(”1”)) { true } else { false } If Apply Literal Literal Select List Constant(true) LiteralLiteral Constant(false) Name(”$eq$eq”) Constant(”1”)Constant(1)
  13. 13. if(1.$eq$eq(”1”)) { true } else { false } If Expr Expr Expr case class If(cond: Expr, then: Expr, else: Expr)
  14. 14. So, We have a functionrunning at compile time Transforming an AST to a new AST what do we do with it?
  15. 15. Ideas for of the top of my head out macrosCompile time checking of stringsGeneric no-runtime-overhead toStringSuper efficient loopszero-overhead logger, assert
  16. 16. Ideascompile time checking of strings for macrosdef myMacro(expr: Expr): Expr { if (expr != valid dateformat) throw Error else expr}
  17. 17. Ideas for macros generic toString with zero-overheaddef toStringMacro[T](expr: Expr[T]): Expr[String] { expr for class name of T + each field with public getter}
  18. 18. Time to show some (Awesome) macros Finally! e slides listening fiv (I stopped ago) Photo: Paul Lewis
  19. 19. Resourcescan I learn more? ... where Odersky: NEXT/Lang-NEXT-2012/Reflection-and-Compilers 2012-05-19-ScalaReflect.pdf Daniel Sobral: json-serialization-with-reflection-in.html
  20. 20. Questions?Slides: / the program pageSample project: johan.andren@mejsla.setwitter: @apnylle