From 7a25348ab838a10a0aa1ec043f76dfbdca7db02f Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Thu, 19 Mar 2026 23:22:14 +0100 Subject: [PATCH] Change `isassigned()` to match the Base definition ScopedValues.jl previously used `isassigned()` to check if a `ScopedValue` had a default value, but this differs from Base which uses `isassigned()` to check if a `ScopedValue` is initialized in the current scope. --- src/ScopedValues.jl | 9 +++++++-- test/runtests.jl | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ScopedValues.jl b/src/ScopedValues.jl index f98b319..9d721da 100644 --- a/src/ScopedValues.jl +++ b/src/ScopedValues.jl @@ -66,9 +66,14 @@ Base.eltype(::ScopedValue{T}) where {T} = T """ isassigned(val::ScopedValue) -Test if the ScopedValue has a default value. +Test if the ScopedValue has an assigned value. """ -Base.isassigned(val::ScopedValue) = val.has_default +function Base.isassigned(val::ScopedValue) + val.has_default && return true + scope = current_scope()::Union{Scope, Nothing} + scope === nothing && return false + return haskey((scope::Scope).values, val) +end const ScopeStorage = HAMT{ScopedValue, Any} diff --git a/test/runtests.jl b/test/runtests.jl index 8d51b1d..f7123a1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -131,6 +131,13 @@ end @test ret == 1.23 end +const sval_uninitialized = ScopedValue{Int}() + +@testset "isassigned()" begin + @test !isassigned(sval_uninitialized) + @with sval_uninitialized => 42 @test isassigned(sval_uninitialized) +end + @testset "ScopedThunk" begin function check_svals() @test sval[] == 8