ScopedValue.runWhere not returning scope

Andrew Haley aph-open at littlepinkcloud.com
Thu Jun 6 08:16:46 UTC 2024


On 6/5/24 22:31, Marcin Grzejszczak wrote:

 > I am wondering what if I don't want to use a ThreadLocal but
 > ScopeValue? Is it the tool to solve this problem? Current API allows
 > me to run a lambda within a scope through
 > `ScopedValue.runWhere(scopedValue, "foo", () -> {})`. But what if I
 > wanted to separate this into 2 steps, the scope opening and closing?

The structured nature of a scoped value is a part of its essential
design. It is the main difference between scoped values and
thread-local variables.

If you consider an execution of a program as a downwards-growing
stack, a scoped value is bound to some value at one point in the
stack, and the bound value is visible to all frames below that point.
That is all: there's no need to consider scopes opening and closing.

It's true that this decision makes scoped values less versatile than
thread-local variables, but on the other hand the hard guarantees it
provides make programs easier to reason about. On balance, we consider
the latter more important than the former.

We did consider (and even implement) an API with open() and close()
methods, but we discarded it in order to make the use you describe
impossible.

-- 
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