-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathScopedValues.java
More file actions
executable file
·31 lines (25 loc) · 656 Bytes
/
Copy pathScopedValues.java
File metadata and controls
executable file
·31 lines (25 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.util.concurrent.*;
/// Proof: scoped-values
/// Source: content/concurrency/scoped-values.yaml
record User(String name) {}
record Request(String path) {}
class Handler {
static final ScopedValue<User> CURRENT =
ScopedValue.newInstance();
User authenticate(Request req) {
return new User("Alice");
}
void handle(Request req) {
ScopedValue.where(CURRENT,
authenticate(req)
).run(this::process);
}
void process() {
// use CURRENT.get() in scope
}
}
void main() {
new Handler().handle(new Request("/"));
}