- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个重载方法,它分别接受一个消费者和一个函数对象,并返回一个与相应消费者/函数匹配的泛型类型。我认为这很好,但是当我尝试使用 lambda 表达式调用任一方法时,我收到一个错误,表明对该方法的引用不明确。
基于我对 JLS §15.12.2.1. Identify Potentially Applicable Methods: 的阅读似乎编译器应该知道我的带有 void block 的 lambda 与 Consumer 方法匹配,而我的带有返回类型的 lambda 与 Function 方法匹配。
我整理了以下无法编译的示例代码:
import java.util.function.Consumer;
import java.util.function.Function;
public class AmbiguityBug {
public static void main(String[] args) {
doStuff(getPattern(x -> System.out.println(x)));
doStuff(getPattern(x -> String.valueOf(x)));
}
static Pattern<String, String> getPattern(Function<String, String> function) {
return new Pattern<>(function);
}
static ConsumablePattern<String> getPattern(Consumer<String> consumer) {
return new ConsumablePattern<>(consumer);
}
static void doStuff(Pattern<String, String> pattern) {
String result = pattern.apply("Hello World");
System.out.println(result);
}
static void doStuff(ConsumablePattern<String> consumablePattern) {
consumablePattern.consume("Hello World");
}
public static class Pattern<T, R> {
private final Function<T, R> function;
public Pattern(Function<T, R> function) {
this.function = function;
}
public R apply(T value) {
return function.apply(value);
}
}
public static class ConsumablePattern<T> {
private final Consumer<T> consumer;
public ConsumablePattern(Consumer<T> consumer) {
this.consumer = consumer;
}
public void consume(T value) {
consumer.accept(value);
}
}
}
我还找到了 similar原来是编译器错误的stackoverflow帖子。我的情况非常相似,虽然有点复杂。对我来说,这仍然看起来像一个错误,但我想确保我没有误解 lambdas 的语言规范。我正在使用 Java 8u45,它应该具有所有最新修复。
如果我将方法调用更改为包装在一个 block 中,一切似乎都可以编译,但这会增加额外的冗长性,并且许多自动格式化程序会将其重新格式化为多行。
doStuff(getPattern(x -> { System.out.println(x); }));
doStuff(getPattern(x -> { return String.valueOf(x); }));
最佳答案
这行肯定是模棱两可的:
doStuff(getPattern(x -> String.valueOf(x)));
从链接的 JLS 章节重读此内容:
A lambda expression (§15.27) is potentially compatible with a functional interface type (§9.8) if all of the following are true:
The arity of the target type's function type is the same as the arity of the lambda expression.
If the target type's function type has a void return, then the lambda body is either a statement expression (§14.8) or a void-compatible block (§15.27.2).
If the target type's function type has a (non-void) return type, then the lambda body is either an expression or a value-compatible block (§15.27.2).
对于 Consumer
你有一个 statement expression因为任何方法调用都可以用作语句表达式,即使该方法是非无效的。例如,您可以简单地这样写:
public void test(Object x) {
String.valueOf(x);
}
这毫无意义,但编译完美。你的方法可能有副作用,编译器不知道。例如,是否 List.add
总是返回 true
而没有人关心它的返回值。
当然,这个 lambda 也适用于 Function
,因为它是一个表达式。因此是模棱两可的。如果你有一个表达式,但不是语句表达式,那么调用将毫无问题地映射到Function
:
doStuff(getPattern(x -> x == null ? "" : String.valueOf(x)));
当你把它改成 { return String.valueOf(x); }
,你创建一个 value-compatible block ,因此它与 Function
匹配,但不符合 void 兼容 block 的条件。但是,您也可能遇到 block 问题:
doStuff(getPattern(x -> {throw new UnsupportedOperationException();}));
此 block 同时具有值兼容和 void 兼容的资格,因此您再次有歧义。另一个歧义 block 示例是无限循环:
doStuff(getPattern(x -> {while(true) System.out.println(x);}));
至于 System.out.println(x)
的情况有点棘手。它肯定符合语句表达式的条件,因此可以匹配到Consumer
,但似乎它匹配到表达式以及规范说method invocation是一个表达式。然而它是有限使用的表达like 15.12.3说:
If the compile-time declaration is void, then the method invocation must be a top level expression (that is, the Expression in an expression statement or in the ForInit or ForUpdate part of a for statement), or a compile-time error occurs. Such a method invocation produces no value and so must be used only in a situation where a value is not needed.
所以编译器完全遵循规范。首先,它确定您的 lambda 主体同时被限定为表达式(即使它的返回类型为 void:15.12.2.1 也不异常(exception))和语句表达式,因此它也被视为歧义。
因此对我来说,这两个语句都根据规范进行编译。 ECJ 编译器在此代码上产生相同的错误消息。
一般来说,当您的重载具有相同数量的参数并且仅在可接受的功能接口(interface)方面存在差异时,我建议您避免重载方法。即使这些函数式接口(interface)有不同的数量(例如,Consumer
和 BiConsumer
):你不会遇到 lambda 问题,但可能会遇到方法引用问题。在这种情况下,只需为您的方法选择不同的名称(例如,processStuff
和 consumeStuff
)。
关于Java 8 消费者/函数 Lambda 歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584887/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!