title: Groovy and Java Callbacks / Lambdas (Closures with a type)
body: <p>This is a callback I have in Java (<code>future</code> is a Mongo's <a href="https://github.com/mongodb/mongo-java-driver/blob/3.0.x/driver/src/main/org/mongodb/operation/SingleResultFuture.java" rel="nofollow"><code>SingleFutureResult</code></a>):</p>
  
  <pre><code>future.register(new SingleResultCallback&lt;ArrayList&lt;Document&gt;&gt;() {
      @Override
      void onResult(ArrayList&lt;Document&gt; documents, MongoException e) {
          //do something
      }
   })
  </code></pre>
  
  <p>With Java 8 I can use Lambdas to make it look like this:</p>
  
  <pre><code>future.register((documents, e) -&gt; {
      //do something
  });
  </code></pre>
  
  <p>Neat. However, I would like to call that Java method within a Groovy class. I know Groovy does currently not support jdk8 lambda's syntax, but the docs always stress that they have closures instead. If I try to use a closure in such a case it fails, as the closure is of type <code>Closure</code>, and not of type <code>SingleResultCallback</code>. Is there a way to have something like a closure with a different type? Is there any other way to make this work in a nice way? I know I can use the first solution, but that looks pretty weird within a groovy class.</p>