Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/ScopedValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ struct ScopedFunctor{F}
end
(sf::ScopedFunctor)() = @enter_scope sf.scope sf.f()

"""
get(val::ScopedValue{T1}, default::T2)::Union{T1, T2}

Like the single-argument [`ScopedValues.get`](@ref), but returns the
provided `default` (rather than `nothing`) if `val` has no default.
Also, does not wrap the return in `Some`.
"""
function get(val::ScopedValue{T1}, default::T2) where {T1, T2}
scope = current_scope()
scope === nothing && return isassigned(val) ? val.default : default
scope = scope::Scope
return Base.get(scope.values, val, isassigned(val) ? val.default : default)
end

@deprecate scoped with

end # module ScopedValues
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,15 @@ end
end
sf()
end

const sval_unassigned = ScopedValue{Int}()
@testset "get with default" begin
@test ScopedValues.get(sval_unassigned, nothing) == nothing
@test ScopedValues.get(sval_unassigned, -1) == -1
@with sval_unassigned=>10 begin
@test ScopedValues.get(sval_unassigned, -1) == 10
end
@static if VERSION >= v"1.9"
@test 0 == @allocations ScopedValues.get(sval_unassigned, nothing)
end
end
Loading