<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Ron Pressler" <ron.pressler@oracle.com><br><b>To: </b>"Remi Forax" <forax@univ-mlv.fr><br><b>Cc: </b>"loom-dev" <loom-dev@openjdk.java.net><br><b>Sent: </b>Friday, January 6, 2023 8:38:09 PM<br><b>Subject: </b>Re: [External] : API proposal for StructuredTaskScope<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
<br class="">
<div><br class="">
<blockquote class="">
<div class="">On 5 Jan 2023, at 15:22, <a href="mailto:forax@univ-mlv.fr" class="" target="_blank">
forax@univ-mlv.fr</a> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">----- Original Message -----<br class="">
<blockquote class="">From: "Ron Pressler" <<a href="mailto:ron.pressler@oracle.com" class="" target="_blank">ron.pressler@oracle.com</a>><br class="">
To: "Remi Forax" <<a href="mailto:forax@univ-mlv.fr" class="" target="_blank">forax@univ-mlv.fr</a>><br class="">
Cc: "loom-dev" <<a href="mailto:loom-dev@openjdk.java.net" class="" target="_blank">loom-dev@openjdk.java.net</a>><br class="">
Sent: Thursday, January 5, 2023 3:37:06 PM<br class="">
Subject: Re: [External] : API proposal for StructuredTaskScope<br class="">
</blockquote>
<br class="">
<blockquote class="">We have considered such approaches.<br class="">
<br class="">
One glaring problem is the case of “heterogenous tasks”, i.e. tasks that each<br class="">
return a result of a different type. This is a very common scenario for<br class="">
ShutdownOnFailure.<br class="">
</blockquote>
<br class="">
I believe that using a static factory instead of a constructor (here StructuredTaskScope.of()) solve that pretty well.<br class="">
If no type is explicitly provided, the inference will default to Object which is exactly what you want in case of a shutdown on failure.</div>
</div>
</blockquote>
<br class="">
</div>
<div>I don’t follow. The scenario I had in mind is this:</div>
<div><br class="">
</div>
<div>
<div><font class="" face="Courier New">Response handle() throws ExecutionException, InterruptedException {</font></div>
<div><font class="" face="Courier New">    try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {</font></div>
<div><font class="" face="Courier New">        Future<String>  s1  = scope.fork(() -> fetchS1());</font></div>
<div><font class="" face="Courier New">        Future<String>  s2  = scope.fork(() -> fetchS2());</font></div>
<div><font class="" face="Courier New">        Future<Integer> i1  = scope.fork(() -> fetchI1());</font></div>
<div><font class="" face="Courier New">        Future<</font><span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: "Courier New";" class="">Integer</span><font class="" face="Courier New">> i2  = scope.fork(() -> fetchI2());</font></div>

<div><font class="" face="Courier New">        scope.join()</font><span style="font-family: "Courier New";" class="">.throwIfFailed();</span></div>
<div><font class="" face="Courier New"><br class=""></font></div>
<div><font class="" face="Courier New">        return new Response(s1, s2, i1, i2);</font></div>
<div><font class="" face="Courier New">    }</font></div>
<div><font class="" face="Courier New">}</font></div>
<div><br class="">
</div>
<div>How do you propose to do it when fork returns void?</div></div></blockquote><div><br></div><div>Ok, I get it, if the result values are heterogeneous, as you said, fork() has to return a Supplier.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>  try(var scope = StructuredTaskScope.of(Reducer.firstException().shutdownOnFailure())) {<br>      Supplier<Integer> s1 = scope.fork(() -> fetchS1());<br>      Supplier<Integer> s2 = scope.fork(() -> fetchS2());<br>      Supplier<Integer> i1 = scope.fork(() -> fetchI1());</div><div>      Supplier<Integer> i2 = scope.fork(() -> fetchI2());<br><br>      Optional<Throwable> result = scope.result();<br>      result.ifPresent(e -> { throw new RuntimeException(e); });<br data-mce-bogus="1"></div><div>      return new Response(s1.get(), s2.get(), i1.get(), i2.get());<br data-mce-bogus="1"></div><div>  }<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>firstException() returns the first exception as an Optional<Throwable> and shutdownOnFailure() calls shutdown() if there is a failure.<br data-mce-bogus="1"></div><div>The nice thing is that shutdownOnFailure() is a composable higern order function that can be used not only with firstException() but with any reducers, like toList() by example.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div>
<div><br class="">
</div>
<div>— Ron</div>
</div>
<div>
<div class=""><br class="">
</div>
</div></blockquote><div><br></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div></div></div></body></html>