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 full-size slide

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

    View full-size 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 full-size slide

  4. Macro Methods
    And why should you care?

    View full-size slide

  5. Groovy awesomeness

    View full-size slide

  6. Groovy awesomeness
    Runtime

    meta-programming

    View full-size slide

  7. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

    meta-programming

    View full-size slide

  8. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

    meta-programming
    ( ask someone else :D )

    View full-size slide

  9. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

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

    View full-size slide

  10. Groovy awesomeness
    Runtime

    meta-programming
    Compile-time

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

    View full-size slide

  11. Global AST
    transformations

    View full-size slide

  12. GORM’s
    GlobalDetachedCriteriaASTTransformation

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. Groovy’s
    AstBuilder.buildFromCode

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. What’s in common?

    View full-size slide

  19. They’re global AST
    transformations

    View full-size slide

  20. Scenario
    1. Find a method call

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. 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 full-size slide

  24. Smells like demo

    View full-size slide

  25. What can you implement?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  28. 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 full-size slide

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

    View full-size slide

  30. // your awesome library here
    ?

    View full-size slide