Compiler bug? (was RE: A bit of fun with the Y combinator)
Millies, Sebastian
Sebastian.Millies at softwareag.com
Wed Sep 11 10:41:45 PDT 2013
Hello Zhong,
I was about to test your cheat, but the compiler throws an exception (b106).
Here's the code:
import java.util.function.Function;
public class Zhong {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
System.out.println(n + "! = " + factorialZhong(n));
}
// Zhong Yu's variation, which is *really* cheating :-)
private static <A,B> Function<A,B> fix2(Function<Function<A,B>, Function<A,B>> f)
{
return new Object()
{
Function<A,B> r = a -> f.apply(r).apply(a);
}.r;
}
private static int factorialZhong(int n) {
return fix2(
// Recursive function generator
(Function<Integer,Integer> f) -> (Integer x) -> (x == 0) ? 1 : x * f.apply(x - 1)
)
.apply(
// Argument
n);
}
}
Here's the error message:
Using javac 1.8.0-ea to compile java sources
java: An exception has occurred in the compiler (1.8.0-ea). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
java: java.lang.ClassCastException: com.sun.tools.javac.code.Symbol$ClassSymbol cannot be cast to com.sun.tools.javac.code.Symbol$MethodSymbol
Can anyone reproduce that?
-- Sebastian
-----Original Message-----
From: Zhong Yu [mailto:zhong.j.yu at gmail.com]
Sent: Wednesday, September 11, 2013 6:36 PM
To: Millies, Sebastian
Cc: lambda-dev at openjdk.java.net
Subject: Re: A bit of fun with the Y combinator
My variation, which is *really* cheating :D
public static <A,B> Function<A,B> fix2(Function<Function<A,B>, Function<A,B>> f)
{
return new Object()
{
Function<A,B> r = a -> f.apply(r).apply(a);
}.r;
}
Zhong Yu
Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
More information about the lambda-dev
mailing list