Skip to content

Commit

Permalink
Make ScopedValues.get a method of Base.get
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner authored and vchuravy committed Jan 7, 2025
1 parent 22134ca commit 0911c3a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion base/logging/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ LogState(logger) = LogState(LogLevel(_invoked_min_enabled_level(logger)), logger
const CURRENT_LOGSTATE = ScopedValue{LogState}()

function current_logstate()
maybe = @inline Base.ScopedValues.get(CURRENT_LOGSTATE)
maybe = @inline get(CURRENT_LOGSTATE)
return something(maybe, _global_logstate)::LogState
end

Expand Down
4 changes: 2 additions & 2 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Base.copyto!(fd::BigFloatData, limbs) = copyto!(getfield(fd, :d), offset_p_limbs

include("rawbigfloats.jl")

rounding_raw(::Type{BigFloat}) = something(Base.ScopedValues.get(CURRENT_ROUNDING_MODE), ROUNDING_MODE[])
rounding_raw(::Type{BigFloat}) = something(get(CURRENT_ROUNDING_MODE), ROUNDING_MODE[])
setrounding_raw(::Type{BigFloat}, r::MPFRRoundingMode) = ROUNDING_MODE[]=r
function setrounding_raw(f::Function, ::Type{BigFloat}, r::MPFRRoundingMode)
Base.ScopedValues.@with(CURRENT_ROUNDING_MODE => r, f())
Expand Down Expand Up @@ -1039,7 +1039,7 @@ _convert_precision_from_base(precision::Integer, base::Integer) =
base == 2 ? precision : ceil(Int, precision * log2(base))

_precision_with_base_2(::Type{BigFloat}) =
Int(something(Base.ScopedValues.get(CURRENT_PRECISION), DEFAULT_PRECISION[])) # default precision of the type BigFloat itself
Int(something(get(CURRENT_PRECISION), DEFAULT_PRECISION[])) # default precision of the type BigFloat itself

"""
setprecision([T=BigFloat,] precision::Int; base=2)
Expand Down
17 changes: 8 additions & 9 deletions base/scopedvalues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module ScopedValues

export ScopedValue, with, @with
public get

"""
ScopedValue(x)
Expand Down Expand Up @@ -57,7 +56,7 @@ Base.eltype(::ScopedValue{T}) where {T} = T
Test whether a `ScopedValue` has an assigned value.
See also: [`ScopedValues.with`](@ref), [`ScopedValues.@with`](@ref), [`ScopedValues.get`](@ref).
See also: [`ScopedValues.with`](@ref), [`ScopedValues.@with`](@ref), [`get`](@ref).
# Examples
```jldoctest
Expand Down Expand Up @@ -141,24 +140,24 @@ julia> using Base.ScopedValues
julia> a = ScopedValue(42); b = ScopedValue{Int}();
julia> ScopedValues.get(a)
julia> get(a)
Some(42)
julia> isnothing(ScopedValues.get(b))
julia> isnothing(get(b))
true
```
"""
function get(val::ScopedValue{T}) where {T}
function Base.get(val::ScopedValue{T}) where {T}
scope = Core.current_scope()::Union{Scope, Nothing}
if scope === nothing
val.has_default && return Some{T}(val.default)
return nothing
end
scope = scope::Scope
if val.has_default
return Some{T}(Base.get(scope.values, val, val.default)::T)
return Some{T}(get(scope.values, val, val.default)::T)
else
v = Base.get(scope.values, val, novalue)
v = get(scope.values, val, novalue)
v === novalue || return Some{T}(v::T)
end
return nothing
Expand Down Expand Up @@ -191,7 +190,7 @@ new dynamic scope with `var` set to `val`. `val` will be converted to type `T`.
`@with var=>val expr` is equivalent to `with(var=>val) do expr end`, but `@with`
avoids creating a closure.
See also: [`ScopedValues.with`](@ref), [`ScopedValues.ScopedValue`](@ref), [`ScopedValues.get`](@ref).
See also: [`ScopedValues.with`](@ref), [`ScopedValues.ScopedValue`](@ref), [`get`](@ref).
# Examples
```jldoctest
Expand Down Expand Up @@ -231,7 +230,7 @@ end
Execute `f` in a new dynamic scope with `var` set to `val`. `val` will be converted
to type `T`.
See also: [`ScopedValues.@with`](@ref), [`ScopedValues.ScopedValue`](@ref), [`ScopedValues.get`](@ref).
See also: [`ScopedValues.@with`](@ref), [`ScopedValues.ScopedValue`](@ref), [`get`](@ref).
# Examples
```jldoctest
Expand Down
2 changes: 1 addition & 1 deletion doc/src/base/scopedvalues.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Base.ScopedValues.ScopedValue
Base.ScopedValues.with
Base.ScopedValues.@with
Base.isassigned(::Base.ScopedValues.ScopedValue)
Base.ScopedValues.get
Base.get(::Base.ScopedValues.ScopedValue)
```

## Implementation notes and performance
Expand Down

0 comments on commit 0911c3a

Please sign in to comment.