# Best practice in package globals

**URL:** <https://discourse.julialang.org/t/best-practice-in-package-globals/110517>\
**Category:** General Usage\
**Tags:** package\
**Created:** [February 21, 2024, 2:07pm UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517 "2024-02-21T14:07:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [February 21, 2024, 2:07pm UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/1 "2024-02-21T14:07:28Z")

</div>

I’m developing a package that has a couple of settings that are “global” to the package. Is there a best practice for this kind of thing?

At the moment I have some getters and setters but should I use `ENV`, which would allow use of `withenv`? Something like

```julia
ENV[PACKAGENAME_SETTING] = true

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [February 21, 2024, 2:19pm UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/2 "2024-02-21T14:19:36Z")

</div>

Constant `Ref`s are often used for this:

```julia
const VALUE_A = Ref(42)
const VALUE_B = Ref("auto")
const VALUE_C = Ref(true)

function f(x)
    if VALUE_C[]
        @info VALUE_B[]
    end
    return 2 * x + VALUE_A[]
end

```

Since the `Ref`s are constant, the function is type-stable. However, you can still change the values the `Ref`s hold:

```julia-repl
julia> f(5)
[ Info: auto
52

julia> VALUE_B[] = "manual"
"manual"

julia> VALUE_A[] = 142
142

julia> f(5)
[ Info: manual
152

```

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [February 21, 2024, 2:20pm UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/3 "2024-02-21T14:20:57Z")

</div>

Is there a usual way of exposing these to package users?

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [February 21, 2024, 2:26pm UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/4 "2024-02-21T14:26:35Z")

</div>

Functions are just fine IMHO:

```julia
export set_value_a
set_value_a(x::Integer) = VALUE_A[] = x

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 21, 2024, 2:26pm UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/5 "2024-02-21T14:26:36Z")

</div>

I would just use a custom struct:

```julia
@kwdef mutable struct PackageSettings
     a::Float64 = 1.0
     b::Int = 1
end

const package_settings = PackageSettings()

```

Then the user can do:

```julia
package_settings.a = 2.0

```

or one can define some setter function to make it prettier.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 21, 2024, 2:41pm UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/6 "2024-02-21T14:41:02Z")

</div>

Preferences.jl ?

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [February 22, 2024, 12:52am UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/7 "2024-02-22T00:52:19Z")

</div>

_slaps forehead_ of course there’s a great package that solves this problem!

---

<div class="post-metadata">

**Author:** ![mestinso](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@mestinso](https://discourse.julialang.org/u/mestinso)\
**Post date:** [February 26, 2024, 3:12am UTC](https://discourse.julialang.org/t/best-practice-in-package-globals/110517/8 "2024-02-26T03:12:34Z")

</div>

This thread piqued my interest since I ran into this need recently as well.

Can somebody explain to me the advantage of

`const VALUE_A = Ref(42)` compared to `VALUE_A::Int = 42`

Now, I do understand prior to 1.8, the second approach wasn’t possible. Besides that, I would be curious what the advantage might be?

Edit: I found this stackoverflow post which seems to imply they are equivalent (at least for the simple test in the post): [python 3.x - Julia performance : use Ref()? - Stack Overflow](https://stackoverflow.com/a/76937450). Though if there is an advantage one way or another, I would still be interested in hearing!
