@@ -38,13 +38,15 @@ julia> sval[]
38
38
implementation is available from the package ScopedValues.jl.
39
39
"""
40
40
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)
45
46
end
46
47
47
48
Base. eltype (:: ScopedValue{T} ) where {T} = T
49
+ Base. isassigned (val:: ScopedValue ) = val. has_default
48
50
49
51
const ScopeStorage = Base. PersistentDict{ScopedValue, Any}
50
52
@@ -99,12 +101,12 @@ function get(val::ScopedValue{T}) where {T}
99
101
# Inline current_scope to avoid doing the type assertion twice.
100
102
scope = current_task (). scope
101
103
if scope === nothing
102
- isdefined (val, :initial_value ) && return Some (val. initial_value )
104
+ isassigned (val) && return Some (val. default )
103
105
return nothing
104
106
end
105
107
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 )
108
110
else
109
111
v = Base. get (scope. values, val, novalue)
110
112
v === novalue || return Some (v:: T )
@@ -124,7 +126,7 @@ function Base.show(io::IO, val::ScopedValue)
124
126
print (io, ' (' )
125
127
v = get (val)
126
128
if v === nothing
127
- print (io, " ( undefined) " )
129
+ print (io, " undefined" )
128
130
else
129
131
show (IOContext (io, :typeinfo => eltype (val)), something (v))
130
132
end
0 commit comments