简体   繁体   中英

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..

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, eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM