# Change \`ScopedValue\` default outside of \`with\`

**URL:** <https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631>\
**Category:** General Usage\
**Created:** [July 11, 2025, 7:59am UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631 "2025-07-11T07:59:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 11, 2025, 7:59am UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631/1 "2025-07-11T07:59:39Z")

</div>

Hi All,

I like the recently introduced `ScopedValue` type, but found myself wanting to be able to change it’s default value persistently outside of `ScopedValues.with` calls.

An example for this is that in [GitHub - JuliaPluto/PlutoPlotly.jl](https://github.com/JuliaPluto/PlutoPlotly.jl) I default to converting all numbers to Float32 to speed up and reduce memory of the generated plot objects, but I want to have a flag to be able to potentially disable this behavior when higher precision is needed. (e.g. this issue [Large integers are truncated · Issue #61 · JuliaPluto/PlutoPlotly.jl · GitHub](https://github.com/JuliaPluto/PlutoPlotly.jl/issues/61))

I thought I could achieve this by combining a `RefValue` and a `ScopedValue` toghther in a single type and make it behave the way I want it (i.e. act in most situation as a `ScopedValue` but allow o change its default value persistently).

I put my attempt into this very small package:

> **[GitHub - disberd/ScopedRefValues.jl: Like ScopedValue, but with the possibility of...](https://github.com/disberd/ScopedRefValues.jl)**
>
> Like ScopedValue, but with the possibility of changing it's default value persistently

and I wanted to get feedback on whether:

- There is already a way to achieve this either built in or via other existing packages
- What I am doing in that package is either broken, unsafe or wrong in any other way

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [July 11, 2025, 8:45am UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631/2 "2025-07-11T08:45:08Z")

</div>

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:

```julia
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](https://github.com/disberd/ScopedRefValues.jl/blob/023fb1a97804fceebf43a496d478a85d03a14445/src/ScopedRefValues.jl#L47C1-L47C68) 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

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

```

Which is why I would embrace mutability fully.

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 12, 2025, 5:06pm UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631/3 "2025-07-12T17:06:08Z")

</div>

Thanks @vchuravy for the quick reply.  
I hadn’t indeed thought about using a `RefValue` directly.

I updated the package mockup to follow the suggestion you gave but I am not so sure anymore whether it’s indeed worth it over just explicitly asking users to pass a `RefValue` explicitly to a normal ScopedValue for flags I want to make customizable in my packages.

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [October 19, 2025, 6:36pm UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631/4 "2025-10-19T18:36:44Z")

</div>

I had a similar desire. The `@with` construct is fine for coding but not very convenient for interactive work. My thought was not to have a mutable value but rather to enter and leave scope with separate commands, something like

```julia-auto
julia> @newscope x => 2.5

julia> # some interactive coding

julia> @endscope

```

Based on the answers to my question [How do ScopedValues actually work?](https://discourse.julialang.org/t/how-do-scopedvalues-actually-work/133270) I gather that scope changes are implemented in core Julia and thus something like this cannot be implemented in a package. But perhaps it is doable in principle?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [October 19, 2025, 6:52pm UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631/5 "2025-10-19T18:52:48Z")

</div>

Maybe [ANN: ScopedSettings - ScopedValues with malleable global values](https://discourse.julialang.org/t/ann-scopedsettings-scopedvalues-with-malleable-global-values/130946) meets your needs?

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [October 19, 2025, 7:10pm UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631/6 "2025-10-19T19:10:45Z")

</div>

Yes, that’s kinda what I built ScopedSettings for - a global default value (that can be mutable), integration with Preferences and environment-variables, but behavior like a ScopedValue once a local scope is entered.

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [October 19, 2025, 7:19pm UTC](https://discourse.julialang.org/t/change-scopedvalue-default-outside-of-with/130631/7 "2025-10-19T19:19:49Z")

</div>

Hmm, the semantics are a little different than what I was thinking of but it might serve my needs. How does it differ from the approach described by @disberd and @vchuravy (using a mutable `RefValue` as a `ScopedValue`)?
