Skip to content

Commit 912257a

Browse files
committed
fixup! allow undefined initial_value
1 parent 154d764 commit 912257a

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

base/scopedvalues.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ julia> sval[]
3838
implementation is available from the package ScopedValues.jl.
3939
"""
4040
mutable struct ScopedValue{T}
41-
const initial_value::T
42-
ScopedValue{T}() where T = new()
43-
ScopedValue{T}(val) where T = new{T}(val)
44-
ScopedValue(val::T) where T = new{T}(val)
41+
const has_default::Bool
42+
const default::T
43+
ScopedValue{T}() where T = new(false)
44+
ScopedValue{T}(val) where T = new{T}(true, val)
45+
ScopedValue(val::T) where T = new{T}(true, val)
4546
end
4647

4748
Base.eltype(::ScopedValue{T}) where {T} = T
49+
Base.isassigned(val::ScopedValue) = val.has_default
4850

4951
const ScopeStorage = Base.PersistentDict{ScopedValue, Any}
5052

@@ -99,12 +101,12 @@ function get(val::ScopedValue{T}) where {T}
99101
# Inline current_scope to avoid doing the type assertion twice.
100102
scope = current_task().scope
101103
if scope === nothing
102-
isdefined(val, :initial_value) && return Some(val.initial_value)
104+
isassigned(val) && return Some(val.default)
103105
return nothing
104106
end
105107
scope = scope::Scope
106-
if isdefined(val, :initial_value)
107-
return Some(Base.get(scope.values, val, val.initial_value)::T)
108+
if isassigned(val)
109+
return Some(Base.get(scope.values, val, val.default)::T)
108110
else
109111
v = Base.get(scope.values, val, novalue)
110112
v === novalue || return Some(v::T)
@@ -124,7 +126,7 @@ function Base.show(io::IO, val::ScopedValue)
124126
print(io, '(')
125127
v = get(val)
126128
if v === nothing
127-
print(io, "(undefined)")
129+
print(io, "undefined")
128130
else
129131
show(IOContext(io, :typeinfo => eltype(val)), something(v))
130132
end

test/scopedvalues.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import Base: ScopedValues
44
@testset "errors" begin
55
@test ScopedValue{Float64}(1)[] == 1.0
66
@test_throws InexactError ScopedValue{Int}(1.5)
7-
var = ScopedValue(1)
8-
@test_throws MethodError var[] = 2
7+
val = ScopedValue(1)
8+
@test_throws MethodError val[] = 2
99
with() do
10-
@test_throws MethodError var[] = 2
10+
@test_throws MethodError val[] = 2
1111
end
12-
@test_throws MethodError ScopedValue{Int}()
12+
val = ScopedValue{Int}()
13+
@test_throws KeyError val[]
1314
@test_throws MethodError ScopedValue()
1415
end
1516

@@ -61,6 +62,7 @@ import Base.Threads: @spawn
6162
end
6263

6364
@testset "show" begin
65+
@test sprint(show, ScopedValue{Int}()) == "ScopedValue{$Int}(undefined)"
6466
@test sprint(show, sval) == "ScopedValue{$Int}(1)"
6567
@test sprint(show, ScopedValues.current_scope()) == "nothing"
6668
with(sval => 2.0) do

0 commit comments

Comments
 (0)