リリースノートに新機能がまとめられています。
今回の新機能は、
- Implicit closure coercion
- @Memoized AST transformation for methods
- Bintray's JCenter repository
- Define base script classes with an annotation
- New DelegatingScript base class for scripts
- New @Log variant for the Log4j2 logging framework
- @DelegatesTo with generics type tokens
- Precompiled type checking extensions
- Groovysh enhancements
- OSGi manifests for the “invoke dynamic” JARs
ということなので、1つずつ確認してみましょう。
Implicit closure coercion
クロージャが暗黙的にインターフェースなんかに変換されるってことらしいですね。
適当に検証してみましよう。
以下のコードを、2.2.1で実行します。
ImplicitClosureCoercion.groovy
/*
2.2.x added Implicit closure coercion
*/
interface StringFilter{
def filter(String source);
}
class SemicollonRemover{
static String remove(String source,StringFilter filter){
filter.filter(source)
}
}
println "I'm Groovy $GroovySystem.version"
println SemicollonRemover.remove('hello;world;') {s->s.replaceAll(';','')}
Out
I'm Groovy 2.2.1
helloworld
通ります。
なお、同様のことを2.1.xで実行するには、
println SemicollonRemover.remove('hello;world;') {s->s.replaceAll(';','')}
この部分を、以下のようにします。
println SemicollonRemover.remove('hello;world;' , {s->s.replaceAll(';','')} as StringFilter)
なんとなく書きやすくなっている気がしますね。
次回は、@Memoized AST transformation for methodsです。
とうやらメモ化をしてくれるアノテーションのようですね。