Typesafe promises and java8 functional interfaces

49 views
Skip to first unread message

Ignacio Baca Moreno-Torres

unread,
Sep 24, 2014, 1:56:26 PM9/24/14
to gwtq...@googlegroups.com
Hi,

I made a prototype of typesafe promises here https://github.com/ibaca/gwtquery/commit/4acb13ed368534693ebfa975351b93f53490c3ad, so you can use promise like that.
Deferred
  .when(
    new PromiseFunction<String>() {
      public void f(Deferred<String> dfd) {
        dfd.onResolve("Hi");
      }
    },
    new PromiseFunction<Integer>() {
      @Override
      public void f(Deferred<Integer> dfd) {
        dfd.onResolve(101);
      }
    },
    /* zip style function, based on RxJava zip operator, this avoid complex generics in Deferred/Promise types */
    new Func2<String, Integer, String>() {
      @Override
      public String call(String s, Integer integer) {
        return s + " " + integer;
      }
    })
  .done(new Action1<String>() {
    @Override
    public void call(String o) {
      assertEquals("Hi 101", o);
      finishTest();
      done = true;
    }
  });

And this changes may help a more java8 friendly Promise API, so you can do something like:
Deferred
  .when(
    (dfd) -> dfd.onResolve("Hi"),
    (dfd) -> dfd.onResolve(101),
    (s, integer) -> s + " " + integer)
  .done(o-> {
      assertEquals("Hi 101", o);
      finishTest();
      done = true;
  });

I don't want to remove the Function abstract class and remplace all with a full typesafe gwtquery, I just propose to add some type safety for uses like PromiseRF or PromiseRPC. Example:
final Request<String> request = ...;
final PromiseRF<String> promiseRF = new PromiseRF<>(request);
promiseRF.done(new Action1<String>() {
  @Override
  public void call(String response) {
    // success
  }
});

What do you think? could be added to gwtquery? 

Manuel Carrasco Moñino

unread,
Oct 27, 2014, 6:03:13 AM10/27/14
to gwtq...@googlegroups.com
Hi Ignacio, sorry by my late response,

I have been studding this patch and I see it reasonable, Julien and me also were talking about changing Functions to be more typesafe and this could be a good example of how to deal with it.

Anyway, could you send a pull request, so as we can comment the code and fully discuss it in github?

Thanks

- Manolo



--
You received this message because you are subscribed to the Google Groups "gwtquery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gwtquery+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ignacio Baca Moreno-Torres

unread,
Oct 27, 2014, 7:29:37 PM10/27/14
to gwtq...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages