Skip to content

Commit 3e4b386

Browse files
authored
add some comments about ScopedValue implementation (#52372)
1 parent 641f717 commit 3e4b386

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

base/scopedvalues.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ julia> sval[]
3838
implementation is available from the package ScopedValues.jl.
3939
"""
4040
mutable struct ScopedValue{T}
41-
const has_default::Bool
41+
# NOTE this struct must be defined as mutable one since it's used as a key of
42+
# `ScopeStorage` dictionary and thus needs object identity
43+
const has_default::Bool # this field is necessary since isbitstype `default` field may be initialized with undefined value
4244
const default::T
4345
ScopedValue{T}() where T = new(false)
4446
ScopedValue{T}(val) where T = new{T}(true, val)

test/scopedvalues.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import Base: ScopedValues
44
@testset "errors" begin
55
@test ScopedValue{Float64}(1)[] == 1.0
66
@test_throws InexactError ScopedValue{Int}(1.5)
7-
val = ScopedValue(1)
8-
@test_throws MethodError val[] = 2
9-
with() do
7+
let val = ScopedValue(1)
108
@test_throws MethodError val[] = 2
9+
with() do
10+
@test_throws MethodError val[] = 2
11+
end
12+
end
13+
let val = ScopedValue{String}()
14+
@test_throws KeyError val[]
15+
end
16+
let val = ScopedValue{Int}()
17+
@test_throws KeyError val[]
1118
end
12-
val = ScopedValue{Int}()
13-
@test_throws KeyError val[]
1419
@test_throws MethodError ScopedValue()
1520
end
1621

0 commit comments

Comments
 (0)