简体   繁体   中英

F.Promise - cyclic inference

At first, i tried to cast with Result

(Result) deleteDummy().

it will compaint you can't cast.

But when i change to ok(), it say cyclic inference

I'm getting a bit confuse here. It keeps saying cyclic inference. It seems like the return not compatible.... any help?

public class Delete_Dummy {
    public static F.Promise<Result> delete() {

        F.Promise<Boolean> promise = isDummyThere(); //rest call to check the exist of the dummy

      return promise.map(aBoolean ->
      {
          if(!aBoolean) {
                return badRequest("No..i'm not there");
          }
          return deleteDummy().map(result-> ok()); //<=cyclic inference <<<<
      });
    }

    private static F.Promise<Result> deleteDummy() {
        String url = "some url";
        return WS.url(url)
                .delete()
                .map(response -> {
                    if (response.getStatus() == NO_CONTENT) {

                        return ok("OK..deleted");
                    }
                    return badRequest("you are bad.");
                } // end of Function<WS>
                );
    }
}

I'm not sure why it would say "cyclic inference", but badRequest() seems to have the type Result while deleteDummy().map(result-> ok()) does have the type Promise<Result> . They are not compatible with each other.

If you have a callback that results itself in a promised result, you will have to use flatMap . You can combine it with a non-promise value in your if-else by using pure :

public static F.Promise<Result> delete() {
    F.Promise<Boolean> promise = isDummyThere(); //rest call to check the exist of the dummy

    return promise.flatMap(aBoolean -> {
        if (!aBoolean) {
            return F.Promise.pure(badRequest("No..i'm not there"));
        }
        return deleteDummy().map(result-> ok());
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM