<div dir="ltr"><div dir="ltr">Robert Engels <<a href="mailto:rengels@ix.netcom.com">rengels@ix.netcom.com</a>> ezt írta (időpont: 2023. jún. 2., P, 12:26):<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I think what a previous poster referred to was that a forked task had no ability to retain a scoped variable (eg hold a reference in a static).<br>
<br>
Although this has direct applicability in solving issues with a reusable buffer - it requires assistance from something like Rusts borrow checker so that a reference cannot be retained and only the contents copied - a signal to the developer that this is an expensive operation so do you really want to do this?</blockquote><div><br></div><div>To be honest, I don't really understand what you mean. What I meant is exactly what Andrew wrote: "override the caller's context, as long as that context was a parent of the current context".</div><div><br></div><div>It is not like a borrow checker. The DI framework I used as an example is just a way to show where such need would naturally arise. However, the problem is more general. Consider that you are sharing a resource via SV (please ignore the fact that I'm not hiding it behind an abstraction for the sake of the example):</div><div><br></div><div>```</div><div>ScopedValue<MyResource> resourceRef = ScopedValue.newInstance();<br>void example() {<br>  try (var resource = new MyResource()) {<br>    ScopedValue.where(resourceRef, resource, () -> {<br>      foo();<br>    });<br>  }<br>}<br></div><div>```</div><div><br></div><div>There is nothing special here, `foo` might use the resource shared via an SV. However, later I might realize that `foo` usually does not need the resource, so it would be a waste to create it. So, I want to make the creation of `MyResource` lazy. However, I have problem, because while I could naively do something like this:</div><div><br></div><div>```</div><div>ScopedValue<MyLazyResource> resourceRef = ScopedValue.newInstance();<br>void example() {<br>  try (var resource = new MyLazyResource()) {<br>    ScopedValue.where(resourceRef, resource, () -> {<br>      foo();<br>    });<br>  }<br>}<br></div><div>```</div><div><br></div><div>where `MyLazyResource` will create the resource the first time it is needed, and closes it if it was created. However, there is a problem with the above code: The scope where `MyResource` is created changed. Even worse: It is ill-defined now. And this problem would be solvable with my requested method.</div><div><br></div></div></div>