Java类型推断错误
我在玩用函数表示对的游戏。我有一个Java类型推断错误,java,lambda,types,functional-programming,Java,Lambda,Types,Functional Programming,我在玩用函数表示对的游戏。我有一个Pair版本工作,它有两个相同类型的元素。我接着做了一对a,B。当我编译以下代码时,我得到以下错误消息: fp2.java:34: error: method pairing in class fp cannot be applied to given types; return fp.<A,B>pairing().apply(a).apply(b); ^ required: no arguments fou
Pair
版本工作,它有两个相同类型的元素。我接着做了一对a,B
。当我编译以下代码时,我得到以下错误消息:
fp2.java:34: error: method pairing in class fp cannot be applied to given types;
return fp.<A,B>pairing().apply(a).apply(b);
^
required: no arguments
found: no arguments
reason: actual and formal argument lists differ in length
1 error
请尝试将代码缩减到绝对最小值。如果删除
apply()
,错误消息是否会保留?什么是fp
?我看不到它在代码中的任何地方被定义。@JacobG。多好的一课啊fp
是处理另一个文件中定义的一对a,a
。这就是解决办法!
public class fp2 {
interface First<A,B> {
Function<B,A> apply(A a);
}
interface Second<A,B> {
Function<B,B> apply(A a);
}
interface Pair<A,B> {
A apply(First<A,B> fst);
B apply(Second<A,B> fst);
}
interface I2<A,B> {
Pair<A,B> apply(B b);
}
public static <A,B> I3<A,B> pairing() {
return a -> b -> new Pair<A,B> () {
public A apply(First<A,B> fst) { return fst.apply(a).apply(b); }
public B apply(Second<A,B> snd) { return snd.apply(a).apply(b); }
};
}
public static <A,B> Pair<A,B> pair(A a, B b) {
return fp.<A,B>pairing().apply(a).apply(b);
}
public static <A,B> First<A,B> fst() {
return a1 -> a2 -> a1;
}
public static <A,B> Second<A,B> snd() {
return a1 -> a2 -> a2;
}
public static void main(String[] args) {
Pair<Integer,Double> p1 = pair(1,2.0);
System.out.println(p1.apply(fst()));
System.out.println(p1.apply(snd()));
}
}