ScopedValue structured forking/forwarding API

Andrew Haley aph-open at littlepinkcloud.com
Thu Jul 10 12:52:04 UTC 2025


On 18/06/2025 18:05, Nikita Bobko wrote:
 > ScopedValue.Snapshot is an AutoCloseable object that internally
 > captures all the bindings from the current Thread.

Let me take a step back for a moment to consider the implications of
this.

The Java rules of access control apply here: if a field is not
accessible, then you may not do anything with it. Those Java rules
apply to scoped values, but we add another: if a field is not
accessible to you, you may not extend its lifetime, or cause it to be
accessible in some other context.

By analogy, it's a fundamental principle in law: you can't transfer
title to something you do not own.

We do this because you cannot know the purpose of every element of a
set of scoped values you've just captured. Some of them might be
critical to the integrity of the system, for example the address of a
region of raw memory such as a frame buffer. For this reason we can't
allow a framework to extend the lifetime of a scoped value beyond the
point its binding context ended.

However, it is straightforward to create a scoped value class of your
own that has any properties you wish, including different inheritance
rules. Given a ScopedValue.Carrier that binds some values, start your
thread with aCarrier.run(task). Would this work for you?

Andrew.



More information about the loom-dev mailing list