From 3e4b38684e38a015446253f5752ee9cf840f50cc Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Sun, 3 Dec 2023 13:15:57 +0900 Subject: [PATCH] add some comments about `ScopedValue` implementation (#52372) --- base/scopedvalues.jl | 4 +++- test/scopedvalues.jl | 15 ++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/base/scopedvalues.jl b/base/scopedvalues.jl index 94491294c798a..6e1cc4755d618 100644 --- a/base/scopedvalues.jl +++ b/base/scopedvalues.jl @@ -38,7 +38,9 @@ julia> sval[] implementation is available from the package ScopedValues.jl. """ mutable struct ScopedValue{T} - const has_default::Bool + # NOTE this struct must be defined as mutable one since it's used as a key of + # `ScopeStorage` dictionary and thus needs object identity + const has_default::Bool # this field is necessary since isbitstype `default` field may be initialized with undefined value const default::T ScopedValue{T}() where T = new(false) ScopedValue{T}(val) where T = new{T}(true, val) diff --git a/test/scopedvalues.jl b/test/scopedvalues.jl index c9d376ab05cbd..b2a0e574fe7aa 100644 --- a/test/scopedvalues.jl +++ b/test/scopedvalues.jl @@ -4,13 +4,18 @@ import Base: ScopedValues @testset "errors" begin @test ScopedValue{Float64}(1)[] == 1.0 @test_throws InexactError ScopedValue{Int}(1.5) - val = ScopedValue(1) - @test_throws MethodError val[] = 2 - with() do + let val = ScopedValue(1) @test_throws MethodError val[] = 2 + with() do + @test_throws MethodError val[] = 2 + end + end + let val = ScopedValue{String}() + @test_throws KeyError val[] + end + let val = ScopedValue{Int}() + @test_throws KeyError val[] end - val = ScopedValue{Int}() - @test_throws KeyError val[] @test_throws MethodError ScopedValue() end