Three compilation errors involving closure literals

Mark Mahieu mark at twistedbanana.demon.co.uk
Sat May 17 13:08:21 PDT 2008


Here are three classes I'd expect to be considered valid, but which  
don't compile.  Not sure if I've mentioned any of these previously,  
apologies if so.


1) Using a closure literal on the rhs of the == and != operators  
yields an "incomparable types" error:


public class IncomparableTypes {

     public static void main(String[] args) {

         {=> void} block = {=>};

         boolean result;
         result = {=>} == block;
         result = block == {=>};
     }
}


IncomparableTypes.java:9: incomparable types: { => void} and { => void}
         result = block == {=>};
                          ^
1 error



2) Using a closure literal as the expression operand of the  
instanceof operator results in an "unexpected type" error:


public class UnexpectedType {

     public static void main(String[] args) {

         boolean result = {=>} instanceof Object;
     }
}


UnexpectedType.java:5: unexpected type
found   : { => void}
required: reference
         boolean result = {=>} instanceof Object;
                           ^
1 error



3) Casting a closure literal is also rejected:


public class Cast {

     public static void main(String[] args) {

         foo((Object) {=> "Hello"});
     }

     static void foo(Object o) { }

     static void foo({=>Object} fn) { }
}


Cast.java:5: ')' expected
         foo((Object) {=> "Hello"});
                     ^
Cast.java:5: not a statement
         foo((Object) {=> "Hello"});
                       ^
Cast.java:5: ';' expected
         foo((Object) {=> "Hello"});
                                  ^
3 errors



Regards,

Mark





More information about the closures-dev mailing list