Java mechanisms when used in lambda shutters

Lamdbaj allows you to define closures in the Java language, you can find various examples here

My question is about the basic Java mechanisms when using, for example, to determine whether println closed, the following code is used:

 Closure println = closure(); { of(System.out).println(var(String.class)); } 

This closure can be performed as follows:

 println.apply("foobar"); 

I'm curious what mechanisms in Java will allow the call of of(...).println(...) to become associated with the println instance itself.

Naturally, the lambdaj source code is readable, but I was hoping for a slightly higher level explanation if anyone has it. My reflection skills go down to a bit of introspection and the execution of methods dynamically.

+6
java closures reflection lambdaj
source share
2 answers

Well, of is apparently a static method that is imported statically, so it can be called without including the class name. I expect var be the same. Both methods should return some type that subsequently called the methods:

 public class Printable { public void println(Var var); } public class Fac { public static Printable of(Object o) { return new Printable(o); } public static Var var(Class<?> clazz) { return new Var(clazz); } } 

Suddenly:

 Fac.of(System.out).println(Fac.var(String.class)); 

It is valid Java. Using static imports, hey presto:

 import static Fac.*; of(System.out).println(var(String.class)); 

Curly braces are obviously valid Java, as you can add them to any method to help define lexical bindings. This style of API design is called freely and is best demonstrated by the JMock testing library.

By the way, if it is supposed to introduce closures in Java, this is pretty ridiculous - the syntax is incredibly awful. Their I / O example actually made me laugh out loud. Try Scala !

EDIT - two println calls are connected. I suppose, because the first sequence of calls allows the library to capture the variables that you passed as parameters. They are probably written in some ThreadLocal structure. When you call the (also supposedly static) println method, the library uses this data to actually execute the behavior at a later point. In addition, testing is due to the fact that the EasyMock test environment uses a similar mechanism (which uses Java proxies in the background) to capture the expected values.

+2
source share

I am Mario Fusco, and I am the main developer of the lambdai library.

First of all, I would like to clarify something: lambdaj is not intended to replace any functional language. As I said last week in my speech on the jug of Zurich, if you have a chance to use Scala, go for it and never look back. Here you can find a summary of my speech, which clearly states that:

http://ctpjava.blogspot.com/2009/10/lambdaj-new-trends-in-java.html

I am also a happy Scala developer. But sometimes you just have to develop in Java (in my experience, in the real world, in about 80% of cases you cannot choose which language you should write your code in), in which case some of the lambdaj functions may be useful (or I hope). I just wanted to bring some functionality to Java that are completely missing. Of course, the result is not completely satisfactory mainly because of the restriction imposed by Java itself.

As for the internal lambdaj mechanism, yes, it uses ThreadLocal to achieve this result. If you have other questions, curiosities, or even the best suggestions and constructive criticism about lambdai, you might be interested to register on the lambdaj mailing list here:

http://groups.google.com/group/lambdaj

Bye mario

+10
source share

All Articles