A couple of Bugs
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Mon Jun 6 20:53:58 PDT 2011
Hi Maurizio,
I discovered a few bugs in current compiler, some of them not lambda
specific.
This is test case.
public class Test {
static class Array<A> {
private A[] data;
Array(A[] data) {
this.data = data;
}
public static <A> Array<A> array(final A... a) {
return new Array<A>(a);
}
public A[] array() {
return null;
}
public boolean forAll(final F<A, Boolean> f) {
for (final A x : data)
if (f.f(x))
return true;
return false;
}
}
public interface F<A, B> {
B f(A a);
}
public static <A> Array<A> test() {
return Array.<A>array();
}
public static Array<Character> fromString(final String s) {
List<Character> cs = Collections.emptyList();
for (int i = s.length() - 1; i >= 0; i--)
cs.add(s.charAt(i));
return new Array<Character>(cs.toArray(new Character[0]));
}
public static void main(String[] args) {
final Array<String> a = Array.array("Hello", "There", "what", "DAY",
"iS", "iT");
final boolean b = a.forAll(#{String s -> fromString(s).forAll(#{
Character c -> isLowerCase(c)}) }); //***********
System.out.println(b); // true ("what" provides the only example;
try removing it)
}
}
Result:
Information:Note: Recompile with -Xlint:unchecked for details.
Information:Compilation completed with 3 errors and 1 warning
Information:3 errors
Information:1 warning
F:\ali\projects\java6\src\Test.java
Error:Error:line (46)error: non-static method array() cannot be
referenced from a static context
where A is a type-variable:
A extends Object declared in class Array
Error:Error:line (46)error: incompatible types
required: Array<A>
found: Object[]
where A is a type-variable:
A extends Object declared in method <A>test()
Error:Error:line (61)error: method forAll in class Array<A> cannot be
applied to given types;
required: F<String,Boolean>
found: <lambda>
reason: actual argument <lambda> cannot be converted to F<String,Boolean> by
method invocation conversion
(incompatible return type ?boolean in lambda expression)
where A is a type-variable:
A extends Object declared in class Array
Warning:Warning:Note: F:\ali\projects\java6\src\Test.java uses unchecked
or unsafe operations.
Othar than last case, all compiles with jdk6. This case relates to
boxing-unboxing primitive types in return types.
If I add explicit cast in return type, it compiles:
final boolean b = a.forAll(#{String s -> (Boolean)fromString(s).forall(#{
Character c -> isLowerCase(c)}) });
Best Regards,
Ali Ebrahimi
More information about the lambda-dev
mailing list