Why does `ScopedValue.where(SCOPE_VALUE, val).call()` throw Exception?

Kasper Nielsen kasperni at gmail.com
Thu Mar 23 11:16:58 UTC 2023


Hi,

I'm in a similar situation as Josiah.

It was the first thing I noticed when using ScopedValue.
Why do I have to handle an exception when the code I'm calling
does not throw any checked exceptions?

So I would really like something like
 <R> R supply(Supplier<? extends R> op);
on ScopedValue.Carrier

and maybe rearrange Carrier to
void where(ScopedValue<T> key, T value, Runnable op);
R where(ScopedValue<T> key, T value, Supplier<? extends R> op);
R whereChecked(ScopedValue<T> key, T value, Callable<? extends R> op)
throws Exception;

/Kasper

On Thu, 23 Mar 2023 at 10:59, Andrew Haley <aph-open at littlepinkcloud.com> wrote:
>
> On 3/23/23 02:52, Josiah Noel wrote:
> > Is there a specific reason that call() throws the checked exception, but
> > not run()?
>
> Yes.
>
> The signature of Callable.call() is:   V call() throws Exception
> The signature of Runnable.run() is:    void run()
>
> Something has to catch the exception.
>
> A previous version of the ScopedValue API had the following method. It was intended for your
> situation.
>
>          /**
>           * Run a value-returning operation with this set of ScopeLocals bound to values,
>           * in the same way as {@code call()}.<p>
>           *     If the operation throws an exception, pass it as a single argument to the {@link Function}
>           *     {@code handler}. {@code handler} must return a value compatible with the type returned by {@code op}.
>           * </p>
>           * @param op    the operation to run
>           * @param <R>   the type of the result of the function
>           * @param handler the handler to be applied if {code op} threw an exception
>           * @return the result.
>            */
>          public final <R> R callOrElse(Callable<R> op,
>                                        Function<? super Exception, ? extends R> handler) {
>              try {
>                  return call(op);
>              } catch (Exception e) {
>                  return handler.apply(e);
>              }
>          }
>
> --
> Andrew Haley  (he/him)
> Java Platform Lead Engineer
> Red Hat UK Ltd. <https://www.redhat.com>
> https://keybase.io/andrewhaley
> EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
>


More information about the loom-dev mailing list