Three compilation errors involving closure literals

Mark Mahieu mark at twistedbanana.demon.co.uk
Sat May 17 14:53:39 PDT 2008


Neal,

Great - look forward to seeing it in openjdk :)

Here's one more you may want to look into, in case it's symptomatic  
of something nastier.  This class shouldn't compile anyway, but  
actually triggers an 'internal error' message...


public class InternalError implements {int=>int}, {String=>String} {
	
	public int invoke(int i) {
		return i;
	}
	
	public String invoke(String s) {
		return s;
	}
		
	static <T> void foo(T t, {T=>T} fn) {
		
		fn.invoke(t);
	}
	
	public static void main(String[] args) {
		
		foo(1, new InternalError());
	}
}


InternalError.java:18: internal error; cannot instantiate <T>foo 
(T,javax.lang.function.OO<? extends T,? super T,? extends  
java.lang.Nothing>) at InternalError to (int,InternalError)
                 foo(1, new InternalError());
                    ^
1 error


Regards,

Mark


On 17 May 2008, at 21:19, Neal Gafter wrote:

> Mark-
>
> Thanks for these reports!  I'm currently working on promoting the  
> code base into openjdk, but once that is done I'll start addressing  
> these issues.
>
> Regards,
> Neal
>
> On Sat, May 17, 2008 at 1:08 PM, Mark Mahieu  
> <mark at twistedbanana.demon.co.uk> wrote:
> 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
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20080517/b9114ca4/attachment.html 


More information about the closures-dev mailing list