Change `ScopedValue` default outside of `with`

One of the central premises of ScopedValue is that you observe the same value inside the scoped value as long as you have not executed a with operation along your dynamical execution.

It is totally fine to use a RefValue as a value for a ScopedValue. The only “benefit” I see here is that you avoid sv[][]. I would not implement this by making the default value special, but rather:

struct MutableScopedValue{T}
    sv::ScopedValue{RefValue{T}}
end
  • What I am doing in that package is either broken, unsafe or wrong in any other way

This looks odd ScopedRefValues.jl/src/ScopedRefValues.jl at 023fb1a97804fceebf43a496d478a85d03a14445 · disberd/ScopedRefValues.jl · GitHub since get does return the default value IIRC.

I also don’t like that setindex! is only w.r.t to the default value, this leads to the surprising behavior

with(srv => :x) do
    srv[] = :y
    @assert srv[] === :y # will fail
end

Which is why I would embrace mutability fully.

4 Likes