Question

RxJava's startFuture(...) takes a Func0 (function that takes no arguments) that returns a Future.

However, I have a function that takes 3 arguments, which can be represented as Func3<type, type, type, Future>. I wrote this function that should close over the Func3 and return it as the result of a Func0:

public Func0 convertToFunc0(final Func3<type 1, type 2, type 3, Future> f)
{
    return new Func0() {
        @Override
        public void call() {
            f.call(???);
        }
    };
}

Assuming I have inserted the appropriate types for "type 1", "type 2" etc., how can I access the arguments of f so I can "close" over them? There's not really any documentation that I can find surrounding the functional parts of RxJava, and I've looked at the source but haven't gotten anywhere.

Was it helpful?

Solution

How about

public Func0 convertToFunc0(final Func3<type 1, type 2, type 3, Future> f, final type1 param1, final type2 param2, final type3 param3)
{
    return new Func0() {
        @Override
        public void call() {
            f.call(param1, param2, param3);
        }
    };
}

If param1, param2 and param3 are not thread-safe, you need to take care the potential race condition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top