Skip to main content

recursive Lambda expressions in jdk1.8?

No replies
lucmoreau
Offline
Joined: 2008-10-03
Points: 0

Hi,

In build jdk1.8-b104, the following example compiled and ran fine. It fails to compile in b117,
with message:

examples/Ex7.java:5: error: self-reference in initializer
Int2Int f = i -> ((i==0) ? 1 : i * f.apply(i-1));

package examples;

public class Ex7 {

Int2Int f = i -> ((i==0) ? 1 : i * f.apply(i-1));

public static void main(String [] args) throws Exception {
System.out.println(new Ex7().f.apply(5));
}
}

I am unclear as to whether this is a bug, or whether recursive functions have been disallowed.

Likewise, the following example used to work fine in a build preceding b104 (sorry, can't recall which)
but fails in both b104 and b117 with

examples/Ex6.java:12: error: self-reference in initializer
return i * f.invoke(i-1);

package examples;

public class Ex6 {
interface int2int {
int invoke(int e);
}

public int2int f = (int i) -> {
if (i==0) {
return 1;
} else {
return i * f.invoke(i-1);
}
};

public static void main(String [] args) throws Exception {
System.out.println(new Ex6().f.invoke(5));
}
}

Your help would be appreciated.

Thanks,
Luc