@@ -17,8 +17,8 @@ concurrently.
1717 Scoped values were introduced in Julia 1.11. In Julia 1.8+ a compatible
1818 implementation is available from the package ScopedValues.jl.
1919
20- In its simplest form you can create a [ ` ScopedValue ` ] ( @ref ) with a
21- default value and then use [ ` with ` ] (@ref Base. with) or [ ` @with ` ] ( @ref ) to
20+ In its simplest form you can create a [ ` Base. ScopedValue` ] ( @ref ) with a
21+ default value and then use [ ` Base. with` ] (@ref with) or [ ` Base. @with` ] ( @ref ) to
2222enter a new dynamic scope.
2323
2424The new scope will inherit all values from the parent scope
@@ -54,6 +54,8 @@ f() # 1
5454Now using a ` ScopedValue ` we can use ** dynamic** scoping.
5555
5656``` julia
57+ using Base. ScopedValues
58+
5759x = ScopedValue (1 )
5860f () = @show x[]
5961with (x=> 5 ) do
@@ -70,6 +72,8 @@ and you can set the value of multiple `ScopedValue`s with one call to `with`.
7072
7173
7274``` julia
75+ using Base. ScopedValues
76+
7377const scoped_val = ScopedValue (1 )
7478const scoped_val2 = ScopedValue (0 )
7579
@@ -94,6 +98,8 @@ Since `with` requires a closure or a function and creates another call-frame,
9498it can sometimes be beneficial to use the macro form.
9599
96100``` julia
101+ using Base. ScopedValues
102+
97103const STATE = ScopedValue {State} ()
98104with_state (f, state:: State ) = @with (STATE => state, f ())
99105```
@@ -106,7 +112,9 @@ The parent task and the two child tasks observe independent values of the
106112same scoped value at the same time.
107113
108114``` julia
115+ using Base. ScopedValues
109116import Base. Threads: @spawn
117+
110118const scoped_val = ScopedValue (1 )
111119@sync begin
112120 with (scoped_val => 2 )
@@ -128,7 +136,9 @@ values. You might want to explicitly [unshare mutable state](@ref unshare_mutabl
128136when entering a new dynamic scope.
129137
130138``` julia
139+ using Base. ScopedValues
131140import Base. Threads: @spawn
141+
132142const sval_dict = ScopedValue (Dict ())
133143
134144# Example of using a mutable value wrongly
@@ -161,6 +171,8 @@ are not well suited for this kind of propagation; our only alternative would hav
161171been to thread a value through the entire call-chain.
162172
163173``` julia
174+ using Base. ScopedValues
175+
164176const LEVEL = ScopedValue (:GUEST )
165177
166178function serve (request, response)
189201### [ Unshare mutable state] (@id unshare_mutable_state)
190202
191203``` julia
204+ using Base. ScopedValues
192205import Base. Threads: @spawn
206+
193207const sval_dict = ScopedValue (Dict ())
194208
195209# If you want to add new values to the dict, instead of replacing
@@ -210,6 +224,7 @@ be in (lexical) scope. This means most often you likely want to use scoped value
210224as constant globals.
211225
212226``` julia
227+ using Base. ScopedValues
213228const sval = ScopedValue (1 )
214229```
215230
@@ -218,7 +233,9 @@ Indeed one can think of scoped values as hidden function arguments.
218233This does not preclude their use as non-globals.
219234
220235``` julia
236+ using Base. ScopedValues
221237import Base. Threads: @spawn
238+
222239function main ()
223240 role = ScopedValue (:client )
224241
@@ -241,6 +258,8 @@ If you find yourself creating many `ScopedValue`'s for one given module,
241258it may be better to use a dedicated struct to hold them.
242259
243260``` julia
261+ using Base. ScopedValues
262+
244263Base. @kwdef struct Configuration
245264 color:: Bool = false
246265 verbose:: Bool = false
260279Base.ScopedValues.ScopedValue
261280Base.ScopedValues.with
262281Base.ScopedValues.@with
263- Base.isassigned(::ScopedValue)
282+ Base.isassigned(::Base.ScopedValues. ScopedValue)
264283Base.ScopedValues.get
265284```
266285
0 commit comments