Local functions
Rémi Forax
forax at univ-mlv.fr
Wed Feb 10 05:09:03 PST 2010
Le 10/02/2010 02:34, Alex Buckley a écrit :
> Initial reaction: local functions are a step too far, even if they do
> have a straightforward translation to lambdas.
>
> Neal Gafter wrote:
>
>> The traditional distinction between functions and methods is that
>> methods are defined in an object scope, and therefore the object's
>> members are in scope. Functions, on the other hand, are defined in
>> whatever scope they are declared in, and therefore inherit their
>> lexical scope (including, e.g., "this") from their point of
>> declaration.
>>
> And with that, how could anyone argue against lexical 'this'? :-)
>
> My concern about lambdas duplicating the functionality of anon.inner
> classes has led me to think about dropping statement lambdas, keeping
> expression lambdas, and then adding block expressions. (I am avoiding
> saying anything about 'this' in such circumstances.)
>
> Alex
>
Block expressions aren't used elsewhere in Java, do you plan to overhaul
the language
to introduce block expressions with if, for, try/catch, etc ?
There is a simple solution, if you want to write recursive function,
create a static method
and reference it as a lambda:
class A {
static int factorial(int x) {
return (x<= 1) ? 1 : x*factorial.(x-1);
}
public static void main(String[] args) {
#int(int) lambda = A#factorial;
}
}
I like the fact a lambda is just a simple expression/block of code and
if you
want something more, you have to use an inner class or a side static method.
Rémi
More information about the lambda-dev
mailing list