# What is a good design for a settable global with a known type?

**URL:** <https://discourse.julialang.org/t/what-is-a-good-design-for-a-settable-global-with-a-known-type/121770>\
**Category:** Performance\
**Tags:** question, performance, design\
**Created:** [October 26, 2024, 2:13am UTC](https://discourse.julialang.org/t/what-is-a-good-design-for-a-settable-global-with-a-known-type/121770 "2024-10-26T02:13:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakobjpeters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobjpeters/32/207797_2.png) [@jakobjpeters](https://discourse.julialang.org/u/jakobjpeters)\
**Post date:** [October 26, 2024, 2:13am UTC](https://discourse.julialang.org/t/what-is-a-good-design-for-a-settable-global-with-a-known-type/121770/1 "2024-10-26T02:13:52Z")

</div>

In a package, I have a `const s = ""` that is used in a couple of functions. One of these functions doesn’t have a way for a user to specify a different string to use instead. What is a good way to make the value settable?

```julia
a::String = ""
set_a(s) = global a = s

const b = Ref("")
set_b(s) = b[] = s

# The real variable is luckily an actual string
ENV["c"] = ""
set_c(s) = ENV["c"] = s

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 26, 2024, 5:15am UTC](https://discourse.julialang.org/t/what-is-a-good-design-for-a-settable-global-with-a-known-type/121770/2 "2024-10-26T05:15:09Z")

</div>

That’s what scoped values are for:

> **[Scoped Values · The Julia Language](https://docs.julialang.org/en/v1/base/scopedvalues/)**
>
> Documentation for The Julia Language.

They allow you to keep the global constant but still be able to change its value 😁.

```julia-repl
julia> using Base.ScopedValues

julia> const s = ScopedValue("")
ScopedValue{String}("")

julia> with(s => "lorem") do
           s[]
       end
"lorem"

julia> s[]
""

```

Currently they’re not optimized very well by the compiler, so even access (`s[]`) may be too costly. Caveat emptor.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [October 26, 2024, 8:17am UTC](https://discourse.julialang.org/t/what-is-a-good-design-for-a-settable-global-with-a-known-type/121770/3 "2024-10-26T08:17:53Z")

</div>

Maybe Preferences.jl could also be useful?

---

<div class="post-metadata">

**Author:** ![pitsianis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitsianis/32/26588_2.png) [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Post date:** [October 26, 2024, 8:49am UTC](https://discourse.julialang.org/t/what-is-a-good-design-for-a-settable-global-with-a-known-type/121770/5 "2024-10-26T08:49:37Z")

</div>

What if you define the local instances as a named parameter with the default value set to the global value?

---

<div class="post-metadata">

**Author:** ![jakobjpeters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobjpeters/32/207797_2.png) [@jakobjpeters](https://discourse.julialang.org/u/jakobjpeters)\
**Post date:** [October 26, 2024, 8:49am UTC](https://discourse.julialang.org/t/what-is-a-good-design-for-a-settable-global-with-a-known-type/121770/6 "2024-10-26T08:49:45Z")

</div>

This fits the use case better! Thanks 😃
