声纳:用方法引用替换这个 lambda [英] Sonar : Replace this lambda with a method reference

查看:37
本文介绍了声纳:用方法引用替换这个 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码示例

Collection<Number> values = transform(
        getValuatedObjects(),
        input -> getValueProvider().apply(input).getValue());

违反 sonarqube 规则:

尽可能用方法引用替换 lambdas

Replace lambdas with method references when possible

这是声纳错误吗?或者我真的可以使用方法参考吗?

is it a sonar bug ? or can i really use a method reference ?

推荐答案

不能替换 lambda input ->getValueProvider().apply(input).getValue() 带有方法引用而不改变语义.

You can’t replace the lambda input -> getValueProvider().apply(input).getValue() with a method reference without changing the semantics.

方法引用替换单个方法调用,因此它不能简单地替换由多个方法调用组成的 lambda 表达式.

A method reference replace a single method invocation, so it can’t simply replace a lambda expression consisting of more than one method invocation.

input -> 形式的 lambda 表达式;getValueProvider().apply(input) 可以被 getValueProvider()::apply 替换当且仅当 getValueProvider() 的评估时间确实没有关系,因为在 lambda 形式中,该方法在每个 lambda 主体评估时调用,而对于方法引用,它只调用一次并捕获结果.

A lambda expression of the form input -> getValueProvider().apply(input) could be replaced by getValueProvider()::apply if, and only if, the evaluation time of getValueProvider() does not matter as in the lambda form the method is invoked on each lambda body evaluation while for the method reference it is invoked only once and the result captured.

这类似于x->的区别.System.out.println(x)System.out::println 其中读取字段 System.out 的内容发生在不同的时间,但通常是没关系.但你应该知道其中的区别.

This is similar to the difference between x -> System.out.println(x) and System.out::println where reading the contents of the field System.out happens at different times but usually it doesn’t matter. But you should be aware of the difference.

在您的示例中,调用了第三个方法 getValue().使用方法引用来表达这一点的唯一方法需要像 Function 这样的函数式接口,它具有像 andThen 和/或 compose 这样的方法.但是,Java 8 的工作方式需要将第一个方法引用转换为目标接口以调用组合方法,这绝不比您现在拥有的 lambda 表达式更容易阅读:((Function<X,Y>)getValueProvider()::apply).andThen(Y::getValue) 其中 Y 是类型,apply(input) 返回.

In your example, a third method getValue() is invoked. The only way to express that with method references needs a functional interface like Function which has methods like andThen and/or compose. However, the way Java 8 works, that would require casting the first method reference to the target interface to invoke the combining method which would be by no way easier to read that the lambda expression you have now: ((Function<X,Y>)getValueProvider()::apply).andThen(Y::getValue) where Y is the type, apply(input) returns.

请注意,规则说用方法引用替换 lambdas如果可能"这让你有空间说,好吧,这是不可能的",但是,我不确定你有多少那么可以称之为规则"……

Note that the rule says "Replace lambdas with method references when possible" which gives you room to say, "well, here it is impossible", however, I’m not sure how much you can call it a "rule" then…

这篇关于声纳:用方法引用替换这个 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆