[External] : Re: Synchronous executor interface

Pedro Lamarão pedro.lamarao at prodist.com.br
Fri Aug 11 21:22:24 UTC 2023


Em sex., 11 de ago. de 2023 às 15:26, Attila Kelemen <
attila.kelemen85 at gmail.com> escreveu:

> To elaborate more, your executor can be created for any type, but that is
> almost pointless for an executor, because an executor instance has to
> support all types. Similarly you can't create a simplified
> `ExecutorService` as `Function<Callable<T>, Future<T>>`. Such an executor
> would be very limited in usefulness, because to simply put: An executor
> cannot have a type argument.
>

The following compiles just fine.

import java.util.function.Function;
import java.util.function.Supplier;

interface Executor {
    default <T> T run(Supplier<T> s, Function<Supplier<T>, Supplier<T>>
mapper) {
        return mapper.apply(s).get();
    }
}

class Wrap {
    public static void main(String[] args) {
        Executor z = new Executor() {};
        String x = z.run(() -> "hi", Wrap::withMessage);
        System.out.println(x);
    }

    static <T> Supplier<T> withMessage(Supplier<T> s) {
        return () -> {
            System.out.println("before");
            try {
                return s.get();
            } finally {
                System.out.println("after");
            }
        };
    }
}

-- 
Pedro Lamarão
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230811/a30e0cc9/attachment.htm>


More information about the loom-dev mailing list