SlideShare a Scribd company logo
1 of 20
Download to read offline
Macros &
reflection
   in Scala 2.10
About me
Consultant



JVM/Java - 7 years
                     @apnylle
Scala - 1 year
IMPORTANT
WARNING
   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)
Photo: Christian Holmér




What is reflection?   ?
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.
But ...
Hasn’t Scala already got
the Java reflection API?!
No support for Scala constructs!

Type erasure

    List[Int] + compiler = List :(
we want something better!
Also:
Homoiconic(ness) - express the language in itself


One API for reflection, macros (and the

compiler)


Support for reification


No overhead when not used
Talking is cheap...
Let’s Look At
Some Code
What about macros?
wikipedia

”
A macro (from the Greek μακρό for "big" or "far")
in computer science is a rule or pattern that
specifies how a certain input sequence (often a
sequence of characters) should be mapped to a
replacement input sequence (also often a sequence
of characters) according to a defined procedure.



                                                Wat!?!
Ok, Iwhat is ait really?
  but
      Get macro,
        A regular scala function


          AST => AST

        Called by the compiler
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 }
if(1.$eq$eq(”1”)) { true } else { false }

                                     If


                    Apply
                                                    Literal              Literal


         Select               List
                                                     Constant(true)

                                          Literal
Literal                                                               Constant(false)
                  Name(”$eq$eq”)



                                   Constant(”1”)
Constant(1)
if(1.$eq$eq(”1”)) { true } else { false }

                  If


        Expr
                            Expr            Expr




    case class If(cond: Expr,
                  then: Expr,
                  else: Expr)
So, We have a function
running at compile time

      Transforming an
      AST to a new AST
           what do we do with it?
Ideas for of the top of my head
        out
            macros
Compile time checking of strings

Generic no-runtime-overhead toString

Super efficient loops

zero-overhead logger, assert
Ideascompile time checking of strings
       for macros
def myMacro(expr: Expr): Expr {

    if (expr != valid dateformat) throw Error

    else expr

}
Ideas for macros
       generic toString with zero-overhead


def toStringMacro[T](expr: Expr[T]): Expr[String] {

    expr for class name of T +

      each field with public getter

}
Time to
  show some
  (Awesome) macros

                   Finally!
                                   e slides
                     listening fiv
          (I stopped
                        ago)

                                              Photo: Paul Lewis
Resourcescan I learn more?
     ... where
 Odersky: http://channel9.msdn.com/Events/Lang-

 NEXT/Lang-NEXT-2012/Reflection-and-Compilers

 Scalamacros.org: http://scalamacros.org/talks/

 2012-05-19-ScalaReflect.pdf

 Daniel Sobral: http://dcsobral.blogspot.fr/2012/07/

 json-serialization-with-reflection-in.html
Questions?
Slides:
  topconf.com / the program page

Sample project:
    http://github.com/johanandren/macros-2012


mail:   johan.andren@mejsla.se


twitter: @apnylle

More Related Content

What's hot

Scala In The Wild
Scala In The WildScala In The Wild
Scala In The Wild
djspiewak
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
djspiewak
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
Bryan O'Sullivan
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
chenge2k
 

What's hot (19)

A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Functional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsFunctional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and Streams
 
Scala : language of the future
Scala : language of the futureScala : language of the future
Scala : language of the future
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
Practically Functional
Practically FunctionalPractically Functional
Practically Functional
 
Scala In The Wild
Scala In The WildScala In The Wild
Scala In The Wild
 
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Speaking Scala: Refactoring for Fun and Profit (Workshop)Speaking Scala: Refactoring for Fun and Profit (Workshop)
Speaking Scala: Refactoring for Fun and Profit (Workshop)
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-std
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
 
Scala in Practice
Scala in PracticeScala in Practice
Scala in Practice
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
 
Threequals - Case Equality in Ruby
Threequals - Case Equality in RubyThreequals - Case Equality in Ruby
Threequals - Case Equality in Ruby
 
ITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production SwiftITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production Swift
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
 
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03
 

Viewers also liked (6)

Duchess scala-2012
Duchess scala-2012Duchess scala-2012
Duchess scala-2012
 
Async – react, don't wait
Async – react, don't waitAsync – react, don't wait
Async – react, don't wait
 
A-Z Target Audience
A-Z Target AudienceA-Z Target Audience
A-Z Target Audience
 
Target Audience A - Z
Target Audience A - ZTarget Audience A - Z
Target Audience A - Z
 
Async - react, don't wait - PingConf
Async - react, don't wait - PingConfAsync - react, don't wait - PingConf
Async - react, don't wait - PingConf
 
Myfanwy Tristram at Gosh Comics
Myfanwy Tristram at Gosh ComicsMyfanwy Tristram at Gosh Comics
Myfanwy Tristram at Gosh Comics
 

Similar to Macros and reflection in scala 2.10

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
intelliyole
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
pramode_ce
 

Similar to Macros and reflection in scala 2.10 (20)

Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako,
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Introductiontoprogramminginscala
IntroductiontoprogramminginscalaIntroductiontoprogramminginscala
Introductiontoprogramminginscala
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
Scala
ScalaScala
Scala
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
Why is Haskell so hard! (And how to deal with it?)
Why is Haskell so hard! (And how to deal with it?)Why is Haskell so hard! (And how to deal with it?)
Why is Haskell so hard! (And how to deal with it?)
 
Scala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationScala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentation
 

More from Johan Andrén

More from Johan Andrén (18)

Next generation message driven systems with Akka
Next generation message driven systems with AkkaNext generation message driven systems with Akka
Next generation message driven systems with Akka
 
Buiilding reactive distributed systems with Akka
Buiilding reactive distributed systems with AkkaBuiilding reactive distributed systems with Akka
Buiilding reactive distributed systems with Akka
 
Next generation actors with Akka
Next generation actors with AkkaNext generation actors with Akka
Next generation actors with Akka
 
Next generation message driven systems with Akka
Next generation message driven systems with AkkaNext generation message driven systems with Akka
Next generation message driven systems with Akka
 
Reactive stream processing using Akka streams
Reactive stream processing using Akka streams Reactive stream processing using Akka streams
Reactive stream processing using Akka streams
 
Networks and types - the future of Akka
Networks and types - the future of AkkaNetworks and types - the future of Akka
Networks and types - the future of Akka
 
Reactive streams processing using Akka Streams
Reactive streams processing using Akka StreamsReactive streams processing using Akka Streams
Reactive streams processing using Akka Streams
 
Building reactive distributed systems with Akka
Building reactive distributed systems with Akka Building reactive distributed systems with Akka
Building reactive distributed systems with Akka
 
Akka streams - Umeå java usergroup
Akka streams - Umeå java usergroupAkka streams - Umeå java usergroup
Akka streams - Umeå java usergroup
 
Streaming all the things with akka streams
Streaming all the things with akka streams   Streaming all the things with akka streams
Streaming all the things with akka streams
 
Asynchronous stream processing with Akka Streams
Asynchronous stream processing with Akka StreamsAsynchronous stream processing with Akka Streams
Asynchronous stream processing with Akka Streams
 
Scala usergroup stockholm - reactive integrations with akka streams
Scala usergroup stockholm - reactive integrations with akka streamsScala usergroup stockholm - reactive integrations with akka streams
Scala usergroup stockholm - reactive integrations with akka streams
 
VJUG24 - Reactive Integrations with Akka Streams
VJUG24  - Reactive Integrations with Akka StreamsVJUG24  - Reactive Integrations with Akka Streams
VJUG24 - Reactive Integrations with Akka Streams
 
Introduction to akka actors with java 8
Introduction to akka actors with java 8Introduction to akka actors with java 8
Introduction to akka actors with java 8
 
Scala frukostseminarium
Scala frukostseminariumScala frukostseminarium
Scala frukostseminarium
 
Introduction to Akka
Introduction to AkkaIntroduction to Akka
Introduction to Akka
 
Akka frukostseminarium
Akka   frukostseminariumAkka   frukostseminarium
Akka frukostseminarium
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

Macros and reflection in scala 2.10

  • 1. Macros & reflection in Scala 2.10
  • 2. About me Consultant JVM/Java - 7 years @apnylle Scala - 1 year
  • 3. IMPORTANT WARNING 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. Photo: Christian Holmér What is reflection? ?
  • 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. But ... Hasn’t Scala already got the Java reflection API?! No support for Scala constructs! Type erasure List[Int] + compiler = List :(
  • 7. we want something better! Also: Homoiconic(ness) - express the language in itself One API for reflection, macros (and the compiler) Support for reification No overhead when not used
  • 8. Talking is cheap... Let’s Look At Some Code
  • 9. What about macros? wikipedia ” A macro (from the Greek μακρό for "big" or "far") in computer science is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to a replacement input sequence (also often a sequence of characters) according to a defined procedure. Wat!?!
  • 10. Ok, Iwhat is ait really? but Get macro, A regular scala function AST => AST Called by the compiler
  • 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. if(1.$eq$eq(”1”)) { true } else { false } If Apply Literal Literal Select List Constant(true) Literal Literal Constant(false) Name(”$eq$eq”) Constant(”1”) Constant(1)
  • 13. if(1.$eq$eq(”1”)) { true } else { false } If Expr Expr Expr case class If(cond: Expr, then: Expr, else: Expr)
  • 14. So, We have a function running at compile time Transforming an AST to a new AST what do we do with it?
  • 15. Ideas for of the top of my head out macros Compile time checking of strings Generic no-runtime-overhead toString Super efficient loops zero-overhead logger, assert
  • 16. Ideascompile time checking of strings for macros def myMacro(expr: Expr): Expr { if (expr != valid dateformat) throw Error else expr }
  • 17. Ideas for macros generic toString with zero-overhead def toStringMacro[T](expr: Expr[T]): Expr[String] { expr for class name of T + each field with public getter }
  • 18. Time to show some (Awesome) macros Finally! e slides listening fiv (I stopped ago) Photo: Paul Lewis
  • 19. Resourcescan I learn more? ... where Odersky: http://channel9.msdn.com/Events/Lang- NEXT/Lang-NEXT-2012/Reflection-and-Compilers Scalamacros.org: http://scalamacros.org/talks/ 2012-05-19-ScalaReflect.pdf Daniel Sobral: http://dcsobral.blogspot.fr/2012/07/ json-serialization-with-reflection-in.html
  • 20. Questions? Slides: topconf.com / the program page Sample project: http://github.com/johanandren/macros-2012 mail: johan.andren@mejsla.se twitter: @apnylle