Slide 1

Slide 1 text

Macro methods Sergei Egorov @bsideup

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Macro Methods And why should you care?

Slide 5

Slide 5 text

Groovy awesomeness

Slide 6

Slide 6 text

Groovy awesomeness Runtime
 meta-programming

Slide 7

Slide 7 text

Groovy awesomeness Runtime
 meta-programming Compile-time
 meta-programming

Slide 8

Slide 8 text

Groovy awesomeness Runtime
 meta-programming Compile-time
 meta-programming ( ask someone else :D )

Slide 9

Slide 9 text

Groovy awesomeness Runtime
 meta-programming Compile-time
 meta-programming ( ask someone else :D ) Local AST transformations

Slide 10

Slide 10 text

Groovy awesomeness Runtime
 meta-programming Compile-time
 meta-programming ( ask someone else :D ) Local AST transformations Global AST transformations

Slide 11

Slide 11 text

Global AST transformations

Slide 12

Slide 12 text

Ask him!

Slide 13

Slide 13 text

GORM’s GlobalDetachedCriteriaASTTransformation

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Groovy’s AstBuilder.buildFromCode

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

What’s in common?

Slide 20

Slide 20 text

They’re global AST transformations

Slide 21

Slide 21 text

Scenario 1. Find a method call

Slide 22

Slide 22 text

Scenario 1. Find a method call 2. analyse it

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Smells like demo

Slide 26

Slide 26 text

What can you implement?

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

// your awesome library here ?