Effectively final

Steven Simpson ss at comp.lancs.ac.uk
Sat Jul 30 01:24:03 PDT 2011


Hi,

On 30/07/11 08:10, Tim Fox wrote:
> Everything is done via callbacks, which always execute on the context of
> the callee. We also provide helpers which allow you to compose the
> results of callbacks in nice ways e.g.
>
> Something like:
>
> int callback1Result;
> int callback2Result;
> Composer.when(callback1,
> callback2).do(#{sendResponse(callback1Result+callback2Result)})
>
> I.e. when both callback1 and callback2 have fired create a response and
> write it back to the client. Note, no thread blocks during this.

I'll try rewriting that without mutable locals.  To fill in the blanks, 
I'm assuming there's some domain-specific scheduling methods that take 
one or more SAM-type callbacks:

   class Services {
     static Completion foo(Some args, FooResponseHandler action);
     static Completion bar(Some args, BarResponseHandler action);
   }

   interface Completion {
     void act(Runnable action);
   }

I've also made them conventionally return 'Completion' so they can be 
plugged into your helper:

   class Composer {
     static Completion when(Completion... parts);
   }

Finally, to translate your code:

   void context() {
     class Anon {
       int result1, result2;

       void callback1(Some args) {
         // Set result1.
       }

       void callback2(Some args) {
         // Set result2.
       }

       void complete() {
         sendResponse(result1 + result2);
       }
     }

     Anon anon = new Anon();
     Composer.when(Services.foo(args, anon#callback1),
                   Services.bar(args, anon#callback2))
             .act(anon#complete);
   }

I dare say, it's not as convenient as mutable locals, but does it meet 
your needs without being too ugly/clunky?

Cheers,

Steven



More information about the lambda-dev mailing list