STS.handleComplete() is not correctly typed

Attila Kelemen attila.kelemen85 at gmail.com
Sun May 14 18:56:07 UTC 2023


To be fair, STS does not make the promise that you should be able to
do that. The returned `TaskHandle` could be different than what is
being passed to `handleComplete` (though it isn't actually), and it
would be possible for STS to call `handleComplete` without unsafe
casts (besides the return type of `TaskHandle.task()`). That said, it
does unsafe casts leading to the two unsafe types in its `TaskHandle`
implementation:

```
TaskRunner(StructuredTaskScope<? super T> scope, Callable<? extends T> task) {
  this.scope = (StructuredTaskScope<T>) scope;
  this.task = (Callable<T>) task;
}
```

Also, if by `Result<T>`, you mean a sealed interface which could be
success or failure (or whatever else), then it would be awkward
actually, because then you are forcing its `Failure` implementation to
have a completely unused generic parameter which would be confusing.

Remi Forax <forax at univ-mlv.fr> ezt írta (időpont: 2023. máj. 14., V, 20:29):
>
> 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