Sub topic 5
Binding a value for the duration of a call
This chapter assumes you know Java lambdas, static final fields, and how the call stack works. It also assumes you have seen a ScopedValue declared as a static final field, as in the previous chapter.
Declaring is not binding
A declared ScopedValue holds nothing yet. It is a key. A value only exists once you bind the key to something for a limited stretch of code. You do this with ScopedValue.where(...), followed by run(...).
public class RequestContext {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice").run(() -> handle());
}
static void handle() {
System.out.println("Handling request for " + USER.get());
}
}On Java releases where scoped values are still a preview feature, compile and run with --enable-preview. Later chapters cover which releases need it.
How where and run work together
ScopedValue.where(USER, "alice") does not bind anything by itself. It returns a carrier, which is a small object that records "USER maps to alice". Calling run on the carrier does the binding:
- The binding for
USERis created. - The
Runnableyou passed executes. - Everything that code calls, directly or indirectly, sees
USERas"alice". - When
runreturns, the binding is removed.
handle() takes no parameters. It reads USER.get() and finds the value from the caller's scope.
Getting a result back
run takes a Runnable, so it returns nothing. When the scoped code must produce a value, use call on the carrier instead.
public class CallExample {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) throws Exception {
String greeting = ScopedValue.where(USER, "alice")
.call(() -> "Hello, " + USER.get());
System.out.println(greeting);
}
}The call method is declared to throw Exception, so the caller must handle or declare it, as main does here.
The binding is not an assignment
Binding is different from setting a variable. Nothing in the API lets code inside the scope change the bound value. The scoped value is a read-only view for as long as run is active. If you try to read it after run returns, it is unbound again.
public class AfterRun {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice").run(() ->
System.out.println(USER.get())); // alice
System.out.println(USER.isBound()); // false
}
}This is the main difference from ThreadLocal. A ThreadLocal value stays until someone calls remove(). A scoped binding cleans itself up when the scope ends, so there is no try/finally to forget.
Common mistakes
- Calling
where(...)and discarding the carrier. Nothing is bound until you callrunorcallon it. - Expecting a value bound in one
runto be visible in a later, separaterun. Each binding ends with its scope. - Expecting a new
Threadstarted inside the scope to see the binding. Thread behavior is covered in a later chapter.
Exercise
What does ScopedValue.where(USER, "bob") do on its own, and which call actually makes USER.get() return "bob"?
Show answer
It only creates a carrier that records the pairing of USER with "bob". No binding exists yet. Calling run (or call) on the carrier creates the binding for the duration of that call, and USER.get() returns "bob" only inside it.