diff --git a/base/exports.jl b/base/exports.jl index 92525b85c7635..9f88abd167ff1 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -651,11 +651,6 @@ export sprint, summary, -# ScopedValue - with, - @with, - ScopedValue, - # logging @debug, @info, diff --git a/doc/src/base/scopedvalues.md b/doc/src/base/scopedvalues.md index 0de29308c5df8..5e97c8139522a 100644 --- a/doc/src/base/scopedvalues.md +++ b/doc/src/base/scopedvalues.md @@ -18,7 +18,7 @@ concurrently. implementation is available from the package ScopedValues.jl. In its simplest form you can create a [`ScopedValue`](@ref) with a -default value and then use [`with`](@ref Base.with) or [`@with`](@ref) to +default value and then use [`with`](@ref with) or [`@with`](@ref) to enter a new dynamic scope. The new scope will inherit all values from the parent scope @@ -54,6 +54,8 @@ f() # 1 Now using a `ScopedValue` we can use **dynamic** scoping. ```julia +using Base.ScopedValues + x = ScopedValue(1) f() = @show x[] with(x=>5) do @@ -70,6 +72,8 @@ and you can set the value of multiple `ScopedValue`s with one call to `with`. ```julia +using Base.ScopedValues + const scoped_val = ScopedValue(1) const scoped_val2 = ScopedValue(0) @@ -94,6 +98,8 @@ Since `with` requires a closure or a function and creates another call-frame, it can sometimes be beneficial to use the macro form. ```julia +using Base.ScopedValues + const STATE = ScopedValue{State}() with_state(f, state::State) = @with(STATE => state, f()) ``` @@ -106,7 +112,9 @@ The parent task and the two child tasks observe independent values of the same scoped value at the same time. ```julia +using Base.ScopedValues import Base.Threads: @spawn + const scoped_val = ScopedValue(1) @sync begin with(scoped_val => 2) @@ -128,7 +136,9 @@ values. You might want to explicitly [unshare mutable state](@ref unshare_mutabl when entering a new dynamic scope. ```julia +using Base.ScopedValues import Base.Threads: @spawn + const sval_dict = ScopedValue(Dict()) # 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 been to thread a value through the entire call-chain. ```julia +using Base.ScopedValues + const LEVEL = ScopedValue(:GUEST) function serve(request, response) @@ -189,7 +201,9 @@ end ### [Unshare mutable state](@id unshare_mutable_state) ```julia +using Base.ScopedValues import Base.Threads: @spawn + const sval_dict = ScopedValue(Dict()) # 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 as constant globals. ```julia +using Base.ScopedValues const sval = ScopedValue(1) ``` @@ -218,7 +233,9 @@ Indeed one can think of scoped values as hidden function arguments. This does not preclude their use as non-globals. ```julia +using Base.ScopedValues import Base.Threads: @spawn + function main() role = ScopedValue(:client) @@ -241,6 +258,8 @@ If you find yourself creating many `ScopedValue`'s for one given module, it may be better to use a dedicated struct to hold them. ```julia +using Base.ScopedValues + Base.@kwdef struct Configuration color::Bool = false verbose::Bool = false diff --git a/test/scopedvalues.jl b/test/scopedvalues.jl index 7f52707a182f1..36f3d3961e343 100644 --- a/test/scopedvalues.jl +++ b/test/scopedvalues.jl @@ -1,5 +1,5 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -import Base: ScopedValues +using Base.ScopedValues @testset "errors" begin @test ScopedValue{Float64}(1)[] == 1.0