Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Your very first macro method in Apache Groovy

Your very first macro method in Apache Groovy

Sergei Egorov

March 02, 2017
Tweet

More Decks by Sergei Egorov

Other Decks in Programming

Transcript

  1. Macro methods
    Sergei Egorov
    @bsideup

    View Slide

  2. About me
    DevOps Advocate at ZeroTurnaround
    Apache Groovy Committer
    Created MacroGroovy & groovy macro methods

    View Slide

  3. About me
    DevOps Advocate at ZeroTurnaround
    Apache Groovy Committer
    Created MacroGroovy & groovy macro methods
    Got drunk and fell yesterday at speakers’ dinner, lol :D

    View Slide

  4. Macro Methods
    And why should you care?

    View Slide

  5. Groovy awesomeness

    View Slide

  6. Groovy awesomeness
    Runtime

    meta-programming

    View Slide

  7. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

    meta-programming

    View Slide

  8. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

    meta-programming
    ( ask someone else :D )

    View Slide

  9. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

    meta-programming
    ( ask someone else :D ) Local AST
    transformations

    View Slide

  10. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

    meta-programming
    ( ask someone else :D ) Local AST
    transformations
    Global AST
    transformations

    View Slide

  11. Global AST
    transformations

    View Slide

  12. Ask him!

    View Slide

  13. GORM’s
    GlobalDetachedCriteriaASTTransformation

    View Slide

  14. def criteria = new DetachedCriteria(Person).build {
    eq 'lastName', 'Simpson'
    }
    def bartQuery = criteria.build {
    eq 'firstName', 'Bart'
    }

    View Slide

  15. def criteria = new DetachedCriteria(Person).build {
    eq 'lastName', 'Simpson'
    }
    def bartQuery = criteria.build {
    eq 'firstName', 'Bart'
    }
    Aha! Macro method!!1

    View Slide

  16. Groovy’s
    AstBuilder.buildFromCode

    View Slide

  17. def result = new AstBuilder().buildFromCode {
    while (true) {
    x++;
    continue
    }
    }

    View Slide

  18. def result = new AstBuilder().buildFromCode {
    while (true) {
    x++;
    continue
    }
    }
    Macro method!

    View Slide

  19. What’s in common?

    View Slide

  20. They’re global AST
    transformations

    View Slide

  21. Scenario
    1. Find a method call

    View Slide

  22. Scenario
    1. Find a method call
    2. analyse it

    View Slide

  23. Scenario
    1. Find a method call
    2. analyse it
    3. Using its arguments, produce new Expression

    View Slide

  24. Scenario
    1. Find a method call
    2. analyse it
    3. Using its arguments, produce new Expression
    4. Replace that method call with what we got in #3

    View Slide

  25. Smells like demo

    View Slide

  26. What can you implement?

    View Slide

  27. def fact(num) {
    return match(num) {
    String >> fact(num.toInteger())
    (0 | 1) >> 1
    2 >> 2
    _ >> _ * fact(_ - 1)
    }
    }
    Pattern Matching

    View Slide

  28. def fact(num) {
    return match(num) {
    String >> fact(num.toInteger())
    (0 | 1) >> 1
    2 >> 2
    _ >> _ * fact(_ - 1)
    }
    }
    Pattern Matching
    Scala? ewww!

    View Slide

  29. View Slide

  30. def fact(num) {
    return match(num) {
    when String then fact(num.toInteger())
    when 0 or 1 then 1
    when 2 then 2
    orElse it * fact(it - 1)
    }
    }
    DSL-like Pattern Matching

    View Slide

  31. doWithData {
    assert a + b == c
    where:
    a | b || c
    1 | 2 || 3
    4 | 5 || 9
    7 | 8 || 15
    }
    Spock-like data tables

    View Slide

  32. // your awesome library here
    ?

    View Slide