I'm currently playing with Vert.x in Java and noticed that the examples in the documentation use lambdas extensively as callback parameters. For example:
NetServer server = vertx.createNetServer();
server.listen(1234, "localhost", res -> {
if (res.succeeded()) {
System.out.println("Server is now listening!");
} else {
System.out.println("Failed to bind!");
}
});
Looking into the documentation of listen functions shows the following:
NetServer listen(int port,
String host,
Handler<AsyncResult<NetServer>> listenHandler)
My question is how does the JVM have a chance to deduce generic data types such as Handler<AsyncResult<NetServer>> out of such non-informative objects such as res? This seems fine for languages like JavaScript that do duck-typing, but for languages like Java that do strong typing, it's not as obvious for me. If we use an anonymous class instead of a lambda, all data types would be on the plate.
--EDIT-- As already explained by @Zircon, probably better example from Vert.x documentation would be following declaration:
<T> void executeBlocking(Handler<Future<T>> blockingCodeHandler,
Handler<AsyncResult<T>> resultHandler)
with example of usage from docs:
vertx.executeBlocking(future -> {
// Call some blocking API that takes a significant amount of time to return
String result = someAPI.blockingMethod("hello");
future.complete(result);
}, res -> {
System.out.println("The result is: " + res.result());
});
Where type of is not available, thus only methods available on Future and AsyncResults can be used.