Eclipse Java 8 Auto Complete for Lambda Expressions

Studied java8 threads in eclipse. Found behavior below Have a list of Student, stuList objects. I have such an expression.

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

If I write p -> p. and then try help with eclipse, the methods will not be shown. But if you add LHS, double d = , then help with the contents in p -> p. Displays all available methods.

I expect the eclipse to understand that I am working on a student object, even if I do not provide LHS. In the first case, if I write a method myself, the code compiles in order. Only autocomplete does not work

Any pointers to this? And why does adding LHS allow eclipse to infer type?

Using eclipse: Mars ..

+8
java eclipse java-8
source share
1 answer

Completion at this position requires that type p be known. As long as the code is syntactically correct, p is output to Student , as pointed out by @Tunaki. However, as soon as you enter '.' the code is too broken, the text does not appear on p . (Side note: text hover and completion are computed by various compiler calls with different contextual information, so they will not always see exactly the same information).

While for a human reader, the body of a lambda may seem inconsequential for type p inference, the conclusion cannot continue without knowing, for example, whether the lambda is void compatible and / or value compatible. p. is not an expression that could help answer this question.

Having the right type of goal is an important contribution to type inference, so in general terms, you shouldn't be surprised that adding LHS improves the situation. However, I have no ready-made explanation of why this is what affects code completion in this case.

All this should only illustrate (at a very high level) why Eclipse behaves the way it does. Improvement is always possible, even if it is associated with extreme complexity, since type inference by incomplete code essentially makes - we request a higher-order conclusion: a conclusion about which of several possible conclusions can give the most likely results.

The good thing about an open source tool: you can help improve it constantly, with well-written bug reports or even code contributions. After seeing the error reports referenced by @ the8472 without an answer, I simply postponed the comment to return them to the radar. Users should not hesitate to send a command to error messages at appropriate time intervals, community requirements are related to setting priorities. Code completion inside lambda bodies is a hot topic on the team's agenda.

+7
source share

All Articles