Name of lambda parameters (original) (raw)

Remi Forax forax at univ-mlv.fr
Tue Jan 8 06:40:37 PST 2013


On 01/08/2013 03:15 PM, Tim Peierls wrote:

On Tue, Jan 8, 2013 at 3:14 AM, Remi Forax <forax at univ-mlv.fr_ _<mailto:forax at univ-mlv.fr>> wrote:

On 01/07/2013 07:16 PM, Brian Goetz wrote: I think this goes into the general bucket of "you can't do reflection below the method level in Java", and lambda expressions are expressions and therefore below the method level. Is lambda somehow special here? On Mon, Jan 7, 2013 at 5:33 PM, Remi Forax <forax at univ-mlv.fr_ _<mailto:forax at univ-mlv.fr> <mailto:forax at univ-mlv.fr_ _<mailto:forax at univ-mlv.fr>>> wrote: Lambda are anonymous functions, so yes, they are special. They can be called, have parameters, like methods so reflection should work otherwise you can't specify meta-protocol using lambdas and having JEE being able to use lambdas seems a good idea. On 01/08/2013 12:16 AM, Tim Peierls wrote: What does "you can't specify meta-protocol using lambdas" mean, and why is it a reason to add further complexity?

In JEE context, a meta-protocols is specified usually by adding annotations classes or methods or method parameters to add dependency injection, SQL query mapping, JSON mapping, etc. By example, with lambdas you can write an easy to use API that let you decode JSON in a 'streamy' way, If I want to parse: [...some JSON with firstName, age, phoneNumber, etc. ...] JSONStream stream = ... (outputStream); stream.parse((String firstName, int age, JSONStream phoneNumber) -> { System.out.println(firstName + " " + age); phoneNumber.parse((String type, String number) -> { System.out.println(number); } }); for that, you need a way to do reflection on parameter names (and annotations). If Java had function types, this would make more sense. Sounds like you want reflection to help address what you think of as a deficiency of the language.

No, it has nothing to do with function type.

It's OK if there are still some things that can't be written more compactly with lambdas. You can already implement something like this in Java 7: stream.parse(new Object() { String firstName; int age; void parse(JSONStream phoneNumber) { System.out.println(firstName + " " + age); phoneNumber.parse(new Object() { String type; String number; void parse() { System.out.println(number); } }); } });

Your example doesn't work if you want the address too (more than one sub item), and will create too many objects.

I don't think the added complexity of specifying reflection for lambdas justifies the marginal (at best) improvement in readability that lambdas provide in this example.

We should have a way to get lambda parameter names and parameter annotations at least just for completeness.

--tim

Rémi



More information about the lambda-libs-spec-observers mailing list