CompletableFuture
Remi Forax
forax at univ-mlv.fr
Thu Nov 29 08:02:42 PST 2012
On 11/29/2012 04:19 PM, Doug Lea wrote:
> On 11/29/12 09:53, Remi Forax wrote:
>>
>> again Future<? extends U>.
>
> Thanks.
>
>>
>> I think that CompletableFuture<?> is better as return type than
>> CompletableFuture<Void>
>> otherwise users may want to declare a local variable Void.
>
> We end up using explicit Void params in FJ stuff a lot
> and people seem OK with it, so we might as well make these
> use the same conventions unless anyone can think of a reason
> otherwise.
j.u.c.ExecutorService use the <?> convention, see submit(Runnable),
so now I let you choose if you perfer to be inconsistent with
ExecutorService or FJ :)
>
>>> /**
>>> * Creates and returns a CompletableFuture that is completed with
>>> * the result of the given function of either this or the other
>>> * given CompletableFuture's results if/when either complete
>>> * normally.
>>> *
>>> * @return the new CompletableFuture
>>> */
>>> public <U> CompletableFuture<U> or(CompletableFuture<? extends
>>> T> x,
>>> BiFunction<? super T,? super
>>> T, ?
>>> extends U> fn);
>>
>> Thinking a little more about this one, fn should be a Function not a
>> BiFunction
>> given that only one of the two completables needs to complete.
>
> Yes, thanks. (I fixed this before but then overwrote with old version;
> oops.)
>
>>> /**
>>> * Returns the value if completed, else the given valueIfAbsent.
>>> */
>>> public T getNow(T valueIfAbsent);
>
>> still think it should use a supplier as parameter and not the value.
>
>
> The motivating use case is to allow simplification of
> polling-like constructions where you'd otherwise need to
> first check isDone, and then get(), but could replace with:
> while(t.getNow(NOTREADY) == NOTREADY) { doSomethingElse(); }
> in which case trapping into a function is not at all helpful.
you mean,
XXX value;
while((value = t.getNow(NOTREADY)) == NOTREADY) { doSomethingElse(); }
> And I can't think of other cases not involving plain get()
> where this method is of much use.
Ok, given that I can write getNow(supplier) from getNow(valueIfAbsent),
your signature is better than mine.
public static <T> T getNow(CompletableFuture<Object> future, Supplier<?
extends T> supplier) {
Object value;
if ((value = future.getNow(NOT_READY) == NOT_READY) {
return supplier.supply();
}
return (T)value;
}
>
> -Doug
>
>
Rémi
More information about the lambda-libs-spec-observers
mailing list