ScopedValue: Capturing current bindings

David Lloyd david.lloyd at redhat.com
Thu Jun 1 17:02:02 UTC 2023


What would happen if there was some context which is implemented in some
way *other* than ScopedValue? For example, using thread locals, or a field
on Thread, or something like that?

On Thu, Jun 1, 2023 at 11:38 AM Attila Kelemen <attila.kelemen85 at gmail.com>
wrote:

> Let's assume we have a static method like this in `ScopedValue`:
>
> ```
> public static void withCapturedContext(
>     Consumer<? super CapturedScopedValueContext> task
> ) { ... }
> ```
>
> where `CapturedScopedValueContext` is defined this way:
>
> ```
> public interface CapturedScopedValueContext {
>   <T> T inContext(Callable<? extends T> task);
> }
> ```
>
> The method `withCapturedContext` captures the current scoped value
> bindings, then immediately calls its argument (task) with an instance of
> `CapturedScopedValueContex` which will run tasks with the captured scope
> value bindings (when calling its `inContext` method). However,
> `CapturedScopedValueContext.inContext` validates if the captured scope
> value bindings is the same as the current one, or the parent of the current
> scoped value bindings, and if it is not, then fails with an ISE.
>
> That is, this could be an unoptimized implementation of
> `withCapturedContext` (with the only caveat that currently `runWith`
> doesn't have a static variant):
>
> ```
> public static void withCapturedContext(
>     Consumer<? super CapturedScopedValueContext> task
> ) {
>   var capturedBindings = scopedValueBindings();
>   task.accept(new CapturedScopedValueContext() {
>     @Override
>     public <T> T inContext(
>         Callable<? extends T> inContextTask
>     ) {
>       scopedValueBindings().verifyValidContext(capturedBindings);
>       return runWith(capturedBindings, inContextTask);
>     }
>   });
> }
>
> static final class Snapshot {
>     // ...
>
>     void verifyValidContext(Snapshot checked) {
>         for (var current = this; current != null; current = current.prev) {
>             if (current == checked) {
>                 return;
>             }
>         }
>         throw new IllegalStateException("Invalid context.");
>     }
> }
> ```
>
> I hope this clarifies what functionality I'm missing.
>
>
> Andrew Haley <aph-open at littlepinkcloud.com> ezt írta (időpont: 2023. jún.
> 1., Cs, 15:50):
>
>> On 6/1/23 12:58, Attila Kelemen wrote:
>> >> However, any structure that allows a scoped value to live after its
>> binding scope has terminated is out of the question because it breaks a
>> fundamental invariant.
>> > I do not want this at all, and never suggested to have this
>> > possibility. In fact, I agree that such access should not be allowed.
>>
>> I'm sorry, trying to understand what you want is exhausting.
>>
>> Please provide a concise and precise definition of what your API to
>> capture current scoped values would do. I do not need any examples
>> of how it might be used, just an operational definition of the
>> CapturedScopedValueContext API.
>>
>> --
>> 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
>>
>>

-- 
- DML • he/him
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230601/08477da5/attachment.htm>


More information about the loom-dev mailing list