あなたは動作しませんどのような再帰的な型定義
<G extends Function<G, IntUnaryOperator>> IntUnaryOperator g(G g) {
return g.apply(g);
}
とジェネリックメソッドを宣言することができ、G
にラムダ式を割り当て、ラムダ式でこのメソッドを呼び出すことです。 The specificationは
15.27.3と言います。ラムダ式
ラムダ式のタイプは、Tは、機能インタフェースタイプである場合(§9.8)...
とG
が割り当てコンテキスト、呼び出しコンテキスト、またはターゲット型Tとコンテキストを鋳造で互換性があります機能的なインタフェースではなくタイプパラメータです。G
の実際のインタフェースタイプを推論する方法はありません。まだ動作
、あなたはラムダ式の実際のインタフェースG
使用する場合:
IntUnaryOperator Y(Function<IntUnaryOperator, IntUnaryOperator> f) {
return g((G)g -> f.apply(x -> g.apply(g).applyAsInt(x)));
}
// renamed the type parameter from G to F to avoid confusion
<F extends Function<F, IntUnaryOperator>> IntUnaryOperator g(F f) {
return f.apply(f);
}
// can't get rid of this interface
interface G extends Function<G, IntUnaryOperator> {/**/}
または
IntUnaryOperator fact = Y(rec -> n -> n == 0 ? 1 : n * rec.applyAsInt(n - 1));
IntUnaryOperator Y(Function<IntUnaryOperator, IntUnaryOperator> f) {
return this.<G>g(g -> f.apply(x -> g.apply(g).applyAsInt(x)));
}
// renamed the type parameter from G to F to avoid confusion
<F extends Function<F, IntUnaryOperator>> IntUnaryOperator g(F f) {
return f.apply(f);
}
// can't get rid of this interface
interface G extends Function<G, IntUnaryOperator> {/**/}
をだから、方法g
インターフェイスG
への依存関係のないと総称ですが、インタフェースはラムダ式のターゲット型として使用する必要があります。
私はそれが不可能だと確信しています。 Javaでは、特定の汎用パラメータが機能インタフェースAFAIKであることを要求する方法はありません。 – Ryan