Hi Paul,
you can achieve the same result with lambdaj as it follows:
int x = 10;
add(int y) { return x + y; }
Closure<Integer> incByX = closure(Integer.class) {
of(this).add(var(integer.class));
}
incByX.apply(4); // return 14
x = 20;
incByX.apply(5); // return 25
Of course the syntax is not concise and elegant as it could be in
groovy or in scala, but it is the best I was able to do given the Java
constraints and limitations. As I wrote in some former post, lambdaj
has not been developed as an alternative for these languages. I am an
happy Scala developer as well, but at the moment I am obliged to use
Java during about the 80% of my working day. That's why a tried to add
a feature in my library to "simulate" what a closure can do in a real
functional language.
I suppose that I don't need to say that I'd never implement this
feature if it was already available in the language (as I think it
should be). Actually I will be happy to throw away lambdaj when (and
if) we will have closures in Java. And this is exactly the point of my
first email. I didn't want to focus the attention on lambdaj, but just
underline the fact that I am participating in discussions about
closures in Java since late 2007 and now it seems that they will be
available in late 2010 (maybe). I admit that the 2 things are hardly
comparable. Anyway I took 3 days to implement "my" closures and I
don't understand why the whole Java community needs 3 years to achieve
a more or less equivalent result.
Bye
Mario
P.S.: I am tired to fight discussing if the lambdaj's closures are
actually closures, function pointer or whatever you want. The
important thing is that people interested in them understand how they
work, their implications and their limitations in order to figure out
case by case if they could be helpful and useful in the given problem
you are going to resolve. Anyway nobody here was able to find a
practical example where a REAL closure can do something that lambdaj's
one can't. And please stop mentioning the Turing machine: nobody here
seems to be an assembler programmer even if it is a Turing complete
language as well. Lambdaj's closures ARE closures under all practical
points of view. And I am not interested in the non practical ones. I
am an engineer and a java programmer. Not a philosopher.
my 2 cents
M.