简体   繁体   English

Eclipse Java 8自动完成Lambda表达式

[英]Eclipse Java 8 Auto completion for Lambda expressions

Was learning java8 streams in eclipse. 在eclipse中学习java8流。 Found the below behaviour Have a List of Student objects , stuList. 找到以下行为有一个Student对象列表,stuList。 I have an expression like this. 我有这样的表达方式。

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

If I write p -> p. 如果我写p -> p. and then try a content assist with eclipse, no methods are shown. 然后尝试使用eclipse进行内容辅助,没有显示任何方法。 But if add a LHS , double d = , then a do content assist on p -> p. 但是如果添加一个LHS, double d = ,那么做内容辅助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. 我希望eclipse能够理解我正在研究一个学生对象,即使我没有提供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? 为什么添加LHS允许eclipse推断类型?

Using eclipse : Mars.. 使用eclipse:Mars ..

Completion in this position requires the type of p to be known. 在此位置完成需要知道p的类型。 As long as the code is syntactically correct , p is inferred to Student as pointed out by @Tunaki. 只要代码在语法上正确的 ,就像@Tunaki所指出的那样, p被推断为Student As soon, however, as you enter the '.' 但是,当你输入''时,很快就会。 the code is just too broken, no text hover appears on p . 代码太破碎了, 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的类型无关,但是在不知道例如λ是否与空白相容和/或价值相容的情况下,推断不能进行。 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. 具有适当的目标类型是类型推断的重要输入,因此一般而言,添加LHS可以改善情况并不令人惊讶。 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. 所有这些只能说明(在很高的层次上)为什么Eclipse的行为方式。 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. 看到@ the8472引用的错误报告已经有一段时间没有回复了,我只是放弃了评论,让它们回到了雷达上。 Users should feel free to ping the team on bug reports in suitable intervals, community demands are relevant for setting priorities. 用户应该在适当的时间间隔内对错误报告中的团队进行ping操作,社区需求与设置优先级相关。 Code completion inside lambda bodies is a hot topic on the team's agenda. lambda机构内部的代码完成是团队议程中的热门话题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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