Closure conversion vs overloading

Neal Gafter neal at gafter.com
Tue Jul 8 00:07:20 PDT 2008


Mark-

This issue has bee fixed.

Regards,
Neal

On Mon, Feb 25, 2008 at 3:04 PM, Mark Mahieu <mark at twistedbanana.demon.co.uk>
wrote:

> Neal,
>
> I'm not quite sure what to make of this one - it feels like a bug but I
> could well be missing something.  I have a class with an overloaded method
> 'foo', one version having a single parameter of type {=>void} and the second
> having a single parameter of type {==>void}.  Intuitively, I'd expect to be
> able to call foo passing a restricted or an unrestricted closure literal,
> since I can do so with an object of restricted or unrestricted function
> type.  However, the unrestricted version fails to compile - javac appears to
> select the 'unrestricted version' of foo, which I didn't expect.
>
> I'm imagining a scenario whereby an API can provide two versions of such a
> method, with the one accepting the restricted type being able to make use of
> concurrent operations, and the compiler would be able to select the most
> appropriate automatically.
>
> The example also attempts to invoke foo using the control invocation
> syntax, but this doesn't compile either - perhaps for the same reason the
> 'normal' invocation doesn't?
>
>
> Example is below:
>
>
> public class Overload {
>
>        public static void main(String[] args) {
>
>                @Shared int restrictedCount = 0;
>                int unrestrictedCount = 0;
>
>                {=>void} restricted = {=> restrictedCount++; };
>                {==>void} unrestricted = {==> unrestrictedCount++; };
>
>                foo(restricted);
>                foo(unrestricted);
>
>                foo({=> restrictedCount++; });
>                foo({==> unrestrictedCount++; });  // fails to compile
>
>                foo() {  // fails to compile
>                        unrestrictedCount++;
>                }
>
>                if (restrictedCount != 2)
>                        throw new AssertionError();
>                if (unrestrictedCount != 3)
>                        throw new AssertionError();
>        }
>
>        static void foo({=>void} block) {
>                block.invoke();
>        }
>
>        static void foo({==>void} block) {
>                block.invoke();
>        }
> }
>
>
> And the compilation errors I get with the latest prototype:
>
> Overload.java:15: cannot assign an unrestricted closure to a restricted
> interface type
>                foo({==> unrestrictedCount++; });
>                     ^
> Overload.java:17: cannot use the control invocation syntax with a method
> that receives a restricted function
>                foo() {
>                      ^
> 2 errors
>
>
>
> Mark
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20080708/7fc9e2f6/attachment.html 


More information about the closures-dev mailing list