STS.handleComplete() is not correctly typed

Remi Forax forax at univ-mlv.fr
Sun May 14 18:28:06 UTC 2023


The STS is typed by the common super type of all callables, but a TaskHandle can be typed by a subtype of that type, so the implementation of STS has to use an unsafe cast somewhere.

To be correctly typed, handleComplete should take a TaskHandle<? extends T>.

Here is an example that does not compile showing the typing issue.

    public static void main(String[] args) throws InterruptedException {
        class SubSTS extends StructuredTaskScope<CharSequence> {
            public <U extends CharSequence> TaskHandle<U> fork(Callable<? extends U> callable) {
                return super.fork(callable);
            }

            @Override
            protected void handleComplete(TaskHandle<CharSequence> handle) {
              super.handleComplete(handle);
            }
        }
        try(var sts = new SubSTS()) {
          var handle = sts.fork(() -> "hello");
          sts.handleComplete(handle);  // called explicitly
          sts.join();
        }
    }

Or handleComplete should not take a task handle but another type (like Result<T>).

Rémi


More information about the loom-dev mailing list