LoginSignup
3
3

More than 5 years have passed since last update.

Groovyのちょっとしたこと「Groovyの2.2がリリースされました。その1」

Last updated at Posted at 2013-12-06

リリースノートに新機能がまとめられています。

今回の新機能は、

  • 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です。

とうやらメモ化をしてくれるアノテーションのようですね。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3