Metadata

When

  • Brainstorming: May 11th-13th 2009
  • Code spikes: May 13th-15th 2009

Where

Paris, France

Participants

  • Brainstorming
    • Paul King
    • Dierk König
    • Guillaume Laforge
    • Peter Niederwieser
    • Jochen Theodorou
  • Code spikes
    • Paul King
    • Guillaume Laforge
    • Jochen Theodorou

Topics

The topics will range between the next coming versions of Groovy: namely Groovy 1.7 and Groovy 2.0. The main highlights will be the following:

Groovy 1.7

Groovy 2.0

Notes

class Expr {}
class Num extends Expr { 
    int value 
    Num(int value) { this.value = value }
}
class Plus extends Expr { 
    Expr left, right 
    Plus(Expr left, Expr right) {
        this.left = left
        this.right = right
    }
}
class Mult extends Expr { 
    Expr left, right 
    Mult(Expr left, Expr right) {
        this.left = left
        this.right = right
    }
}

@Newify([Num, Plus])
def createTerm() {
    Plus(Plus(Num(1), Num(2)), Num(3))
}

def term = createTerm()

// avoid adding a match(closure) method on Object 
// poluting the available names
import static groovy.patternmatching.PatterMatcher.*

match(term) { /* ... */ }


def result = term.match {
    Num() { delegate.value } // or it.value?
    
    // value is a property on the Num POJO
    Num(value) { eval(value) }
    // use the left / right property 
    Plus(left, right) { eval(left) + eval(right) }
    
    // can match a property against a specific value
    Num(value: 5) { 5 }
    
    // add guards examples
    Num(value > 0) { ... }
    
    Plus(left: Plus(left: a, right: 2), right) { a + 2 + right }
    
    // alternation
    Plus(left: a, right: b) | Minus(left: a, right: b) {}
    // fallthrough?
    Plus(left: a, right: b)
    Minus(left: a, right: b) {}
    // can use Object, no need for duck matching
    Object(left, right) // match any object with a left and right 
    
    // factorization
    // factor * a + factor * b == factor * (a + b)
    Plus(left: Mult(left: factor, right: a), right: Mult(left: factor, right: b)) { 
        //Mult(left: factor, right: Plus(left: a, right: b)) 

    }
    
    //
    
    // if nothing matches
    nothing { /* return or do something */ }
    // if anything matches
    anything {}
}