<div dir="ltr">Let's assume we have a static method like this in `ScopedValue`:<br><br>```<br>public static void withCapturedContext(<br>    Consumer<? super CapturedScopedValueContext> task<br>) { ... }<br>```<br><br>where `CapturedScopedValueContext` is defined this way:<br><br>```<br>public interface CapturedScopedValueContext {<br>  <T> T inContext(Callable<? extends T> task);<br>}<br>```<br><br>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.<br><div><br></div><div>That is, this could be an unoptimized implementation of `withCapturedContext` (with the only caveat that currently `runWith` doesn't have a static variant):</div><div><br></div><div>```</div><div>public static void withCapturedContext(<br>    Consumer<? super CapturedScopedValueContext> task<br>) {<br>  var capturedBindings = scopedValueBindings();<br>  task.accept(new CapturedScopedValueContext() {<br>    @Override<br>    public <T> T inContext(<br>        Callable<? extends T> inContextTask<br>    ) {<br>      scopedValueBindings().verifyValidContext(capturedBindings);<br>      return runWith(capturedBindings, inContextTask);<br>    }<br>  });<br>}<br><br>static final class Snapshot {<br>    // ...<br><br>    void verifyValidContext(Snapshot checked) {<br>        for (var current = this; current != null; current = current.prev) {<br>            if (current == checked) {<br>                return;<br>            }<br>        }<br>        throw new IllegalStateException("Invalid context.");<br>    }<br>}<br></div><div>```</div><div><br></div><div>I hope this clarifies what functionality I'm missing.</div></div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Andrew Haley <<a href="mailto:aph-open@littlepinkcloud.com">aph-open@littlepinkcloud.com</a>> ezt írta (időpont: 2023. jún. 1., Cs, 15:50):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 6/1/23 12:58, Attila Kelemen wrote:<br>
>> 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.<br>
> I do not want this at all, and never suggested to have this<br>
> possibility. In fact, I agree that such access should not be allowed.<br>
<br>
I'm sorry, trying to understand what you want is exhausting.<br>
<br>
Please provide a concise and precise definition of what your API to<br>
capture current scoped values would do. I do not need any examples<br>
of how it might be used, just an operational definition of the<br>
CapturedScopedValueContext API.<br>
<br>
-- <br>
Andrew Haley  (he/him)<br>
Java Platform Lead Engineer<br>
Red Hat UK Ltd. <<a href="https://www.redhat.com" rel="noreferrer" target="_blank">https://www.redhat.com</a>><br>
<a href="https://keybase.io/andrewhaley" rel="noreferrer" target="_blank">https://keybase.io/andrewhaley</a><br>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671<br>
<br>
</blockquote></div>