Consider the following 2 controllers written using Play Framework 2.3.8 :
Java8 lambdas:
public static Promise<Result> testLambda() {
final Promise<Boolean> promiseBool = Promise.promise(() -> "TEST".equals("test"));
return promiseBool.map(bool -> ok()).recover(t -> badRequest());
}
"Normal":
public static Promise<Result> test() {
final Promise<Boolean> promiseBool = Promise.promise(new Function0<Boolean>() {
@Override
public Boolean apply() throws Throwable {
return "TEST".equals("test");
}
});
return promiseBool.map(new Function<Boolean, Result>() {
@Override
public Result apply(Boolean bool) throws Throwable {
return ok();
}
}).recover(new Function<Throwable, Result>() {
@Override
public Result apply(Throwable t) throws Throwable {
return badRequest();
}
});
}
The controller written using lambdas gives me this ERROR in eclipse: Type mismatch: cannot convert from F.Promise<Results.Status> to F.Promise<Result>
while the second one doesn't. This happens only when using the recover()
function.
On the other hand, sbt compiles the code without complaining.
Question: Why is this happening and how can it be fixed?
For others searching for a -> REASON:
Based on Salem's answer and this answer: This is an Eclipse bug and has nothing to do with type inference or other < insert you favourite Java bashing here >.
There was a bug related to this in Play Framework but from the discussion it seems that this is more a problem of Java than Play itself. The workaround would be something like this (credit to igmar ):
return promiseBool.map(new Function<Boolean, Result>() {
@Override
public Result apply(Boolean bool) throws Throwable {
Result res = ok();
return res;
}
}).recover(new Function<Throwable, Result>() {
// Same
});
Using return (Result) ok();
may also work.
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.