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. About me DevOps Advocate at ZeroTurnaround Apache Groovy Committer Created

    MacroGroovy & groovy macro methods Got drunk and fell yesterday at speakers’ dinner, lol :D
  2. def criteria = new DetachedCriteria(Person).build { eq 'lastName', 'Simpson' }

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

    def bartQuery = criteria.build { eq 'firstName', 'Bart' } Aha! Macro method!!1
  4. Scenario 1. Find a method call 2. analyse it 3.

    Using its arguments, produce new Expression
  5. 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
  6. def fact(num) { return match(num) { String >> fact(num.toInteger()) (0

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

    | 1) >> 1 2 >> 2 _ >> _ * fact(_ - 1) } } Pattern Matching Scala? ewww!
  8. 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
  9. doWithData { assert a + b == c where: a

    | b || c 1 | 2 || 3 4 | 5 || 9 7 | 8 || 15 } Spock-like data tables