23

这与我对“流减少不兼容类型”的回答有关。我不知道为什么我的建议有效,Holger正确地向我施压。但即使他似乎也没有明确解释它为什么起作用。所以,让我们把它当作自己的问题来问:

以下代码无法编译javac(对于下面指向 ideone 的链接,这是sun-jdk-1.8.0_51,根据http://ideone.com/faq):

public <T> Object with(Stream<Predicate<? super T>> predicates) {
  return predicates.reduce(Predicate::or);
}

这是正确的:从这个流中将两个谓词进行或运算就像是这样写:

Predicate<? super T> a = null;
Predicate<? super T> b = null;
a.or(b);  // Compiler error!

Predicate::or但是,它确实在 intellij 中编译,尽管在方法引用上带有原始类型警告。显然,它也会在 eclipse 中编译(根据原始问题)。

但是这段代码确实:

public <T> Object with(Stream<Predicate<? super T>> predicates) {
  return predicates.map(a -> a).reduce(Predicate::or);
                // ^----------^ Added
}

Ideone demo

尽管我想尝试一下,但我并不完全清楚为什么这会起作用。我的手动解释是,.map(a -> a)它就像一个“演员”,并为类型推断算法提供了更多的灵活性来选择允许reduce应用的类型。但我不确定这种类型到底是什么。

请注意,这不等同于 using .map(Function.identity()),因为它被限制为返回输入类型。ideone demo

任何人都可以解释为什么这可以参考语言规范,或者如果按照 Holger 的建议,它是一个编译器错误?


更详细一点:

方法的返回类型可以更具体一点;我在上面省略了它,这样返回类型上的讨厌的泛型就不会妨碍:

public <T> Optional<? extends Predicate<? super T>> with(
    Stream<Predicate<? super T>> predicates) {
  return predicates.map(a -> a).reduce(Predicate::or);
}

这是用 编译的输出-XDverboseResolution=all。不完全确定这是否是我可以发布以调试类型推断的最相关输出;请告知是否有更好的东西:

Interesting.java:5: Note: resolving method <init> in type Object to candidate 0
class Interesting {
^
  phase: BASIC
  with actuals: no arguments
  with type-args: no arguments
  candidates:
      #0 applicable method found: Object()

Interesting.java:7: Note: resolving method map in type Stream to candidate 0
    return predicates.map(a -> a).reduce(Predicate::or);
                     ^
  phase: BASIC
  with actuals: <none>
  with type-args: no arguments
  candidates:
      #0 applicable method found: <R>map(Function<? super T#1,? extends R>)
        (partially instantiated to: (Function<? super Predicate<? super T#2>,? extends Object>)Stream<Object>)
  where R,T#1,T#2 are type-variables:
    R extends Object declared in method <R>map(Function<? super T#1,? extends R>)
    T#1 extends Object declared in interface Stream
    T#2 extends Object declared in method <T#2>with(Stream<Predicate<? super T#2>>)

Interesting.java:7: Note: Deferred instantiation of method <R>map(Function<? super T#1,? extends R>)
    return predicates.map(a -> a).reduce(Predicate::or);
                         ^
  instantiated signature: (Function<? super Predicate<? super T#2>,? extends Predicate<CAP#1>>)Stream<Predicate<CAP#1>>
  target-type: <none>
  where R,T#1,T#2 are type-variables:
    R extends Object declared in method <R>map(Function<? super T#1,? extends R>)
    T#1 extends Object declared in interface Stream
    T#2 extends Object declared in method <T#2>with(Stream<Predicate<? super T#2>>)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object super: T#2 from capture of ? super T#2

Interesting.java:7: Note: resolving method reduce in type Stream to candidate 1
    return predicates.map(a -> a).reduce(Predicate::or);
                                 ^
  phase: BASIC
  with actuals: <none>
  with type-args: no arguments
  candidates:
      #0 not applicable method found: <U>reduce(U,BiFunction<U,? super T,U>,BinaryOperator<U>)
        (cannot infer type-variable(s) U
          (actual and formal argument lists differ in length))
      #1 applicable method found: reduce(BinaryOperator<T>)
      #2 not applicable method found: reduce(T,BinaryOperator<T>)
        (actual and formal argument lists differ in length)
  where U,T are type-variables:
    U extends Object declared in method <U>reduce(U,BiFunction<U,? super T,U>,BinaryOperator<U>)
    T extends Object declared in interface Stream

Interesting.java:7: Note: resolving method metafactory in type LambdaMetafactory to candidate 0
    return predicates.map(a -> a).reduce(Predicate::or);
                          ^
  phase: BASIC
  with actuals: Lookup,String,MethodType,MethodType,MethodHandle,MethodType
  with type-args: no arguments
  candidates:
      #0 applicable method found: metafactory(Lookup,String,MethodType,MethodType,MethodHandle,MethodType)

Interesting.java:7: Note: resolving method metafactory in type LambdaMetafactory to candidate 0
    return predicates.map(a -> a).reduce(Predicate::or);
                                         ^
  phase: BASIC
  with actuals: Lookup,String,MethodType,MethodType,MethodHandle,MethodType
  with type-args: no arguments
  candidates:
      #0 applicable method found: metafactory(Lookup,String,MethodType,MethodType,MethodHandle,MethodType)
4

1 回答 1

2

除非我在 FunctionalInterface 推断的发生方式上遗漏了一些东西,否则很明显你不能在 Stream < 上调用 reduce ?super Predicate > 因为它没有足够的类型来推断为 BinaryOperator。

方法引用隐藏了故事的一个非常重要的部分,即第二个参数。

return predicates.map(a->a).reduce((predicate, other) -> predicate.or(other));

如果删除对 map 的调用,编译器就没有机会适当地键入流以满足第二个捕获要求。使用map,编译器可以自由地确定满足捕获所需的类型,但是如果没有泛型的具体绑定,这两个捕获只能通过对象流来满足,这很可能是通过map()产生的结果。

现在实现的 Predicate 接口只是构建一个链,但使用预期是一个组合实体。假设采用单个参数,但实际上 AND 和 OR 的性质要求两个参数没有类型保证,因为 Java 泛型的缺点。通过这种方式,API 似乎设计得不够理想。

对 map() 的调用放弃了对类型的控制,从显式的谓词流到编译器可以保证满足所有捕获的类型。

以下两者都满足IDEone中的编译器,通过在 Object 的情况下直接引入足够灵活的类型,或者在 T 的情况下引入已知类型。

public <T> Optional<? extends Predicate<? super T>> with(Stream<Predicate<Object>> predicates)
public <T> Optional<? extends Predicate<? super T>> with(Stream<Predicate<T>> predicates)

Java 泛型仍然需要一种强制捕获类型等价的方法,因为辅助方法显然还不够。

于 2017-07-10T22:05:24.440 回答