Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Java 8 Auto completion for Lambda expressions

Was learning java8 streams in eclipse. Found the below behaviour Have a List of Student objects , stuList. I have an expression like this.

stuList.stream().collect(Collectors.averagingDouble(p -> p.getMarks() ));

If I write p -> p. and then try a content assist with eclipse, no methods are shown. But if add a LHS , double d = , then a do content assist on p -> p. shows all the methods available.

I expect eclipse to understand that i am working on a student object even if i dont provide an LHS. For the first case if i write the method myself, the code compiles fine. Only the auto complete is not working

Any pointers for this? And why adding LHS allows eclipse to infer type?

Using eclipse : Mars..

like image 627
Shabin Hashim Avatar asked Oct 23 '15 14:10

Shabin Hashim


People also ask

Does Java 1.8 support lambda expressions?

Lambda Expressions were added in Java 8.

Is lambda expressions introduced in Java 8?

Introduction. Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .


1 Answers

Completion in this position requires the type of p to be known. As long as the code is syntactically correct, p is inferred to Student as pointed out by @Tunaki. As soon, however, as you enter the '.' the code is just too broken, no text hover appears on p. (Side note: text hover and completion are computed by different compiler invocations with different context information, so they will not always see exactly the same information).

While to the human reader the lambda body may appear as irrelevant for inferring the type of p, inference cannot proceed without knowing, e.g., whether the lambda is void-compatible and/or value-compatible. p. is not an expression that could help answer this question.

Having a proper target type is an important input to type inference, so in general terms it should not surprise that adding an LHS improves the situation. I don't have a ready explanation, though, why exactly it impacts code completion in this case.

All these should only illustrate (at a very high level) why Eclipse behaves the way it does. Improvement is always possible, even if it involves extreme complexity as type inference on incomplete code inherently does - we are requesting no less than higher order inference: inferring which of several possible inferences might produce the most likely results.

The good thing about an Open-Source tool is: you can help in its continuous improvement, by well written bug reports or even code contributions. Seeing the bug reports referenced by @the8472 unanswered for some time I just dropped a comment to put them back on the radar. Users should feel free to ping the team on bug reports in suitable intervals, community demands are relevant for setting priorities. Code completion inside lambda bodies is a hot topic on the team's agenda.

like image 168
Stephan Herrmann Avatar answered Oct 19 '22 15:10

Stephan Herrmann