# How to change constant variable inside a package for testing

**URL:** <https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739>\
**Category:** Package Management\
**Tags:** question, package, constants\
**Created:** [February 25, 2024, 2:57pm UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739 "2024-02-25T14:57:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maysam\_Gholampour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maysam_gholampour/32/218184_2.png) [@Maysam\_Gholampour](https://discourse.julialang.org/u/Maysam_Gholampour)\
**Post date:** [February 25, 2024, 2:57pm UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739/1 "2024-02-25T14:57:36Z")

</div>

I am developing a new package. In package I have defined couple of constant variables. as it is not possible to redefine constant variable, I can’t change the constant variables for testing. we need to reset the Julia. I have an idea but it seems it is not a good way, using parallel processing to run all test files in parallel way. Note that I have defined those variables as constant to improve the performance.

---

<div class="post-metadata">

**Author:** ![Maysam\_Gholampour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maysam_gholampour/32/218184_2.png) [@Maysam\_Gholampour](https://discourse.julialang.org/u/Maysam_Gholampour)\
**Post date:** [February 25, 2024, 3:19pm UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739/2 "2024-02-25T15:19:54Z")

</div>

Also I can use placeholder using Ref, but it is not as good as const. I am not sure whether the using const within a package is a good idea or not.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [February 25, 2024, 8:08pm UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739/3 "2024-02-25T20:08:12Z")

</div>

Something you can do is to replace constant _variables_ with constant _functions_. For example:

```julia
const VERBOSE = false

function foo()
    if VERBOSE
        println("Hi!")
    end
end

```

becomes:

```julia
verbose() = false

function foo()
    if verbose()
        println("Hi!")
    end
end

```

Then in tests, you can redefine the function:

```julia
using MyPackage
MyPackage.verbose() = true

```

---

<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:** [February 25, 2024, 11:41pm UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739/4 "2024-02-25T23:41:41Z")

</div>

I would use a global `Ref`, unless it’s for something _super_ performance sensitive. Looking up the value out of a `Ref` should be quite fast (assuming it is type stable, i.e. the Ref is concretely typed), and likely is not the bottleneck in most situations. It provides a simpler interface and does not require recompiling code to change the setting.

---

<div class="post-metadata">

**Author:** ![Maysam\_Gholampour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maysam_gholampour/32/218184_2.png) [@Maysam\_Gholampour](https://discourse.julialang.org/u/Maysam_Gholampour)\
**Post date:** [February 26, 2024, 2:48am UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739/5 "2024-02-26T02:48:56Z")

</div>

Thanks for sharing your idea. Nice trick.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 26, 2024, 10:46am UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739/6 "2024-02-26T10:46:11Z")

</div>

Why not a typed global instead of a `Ref`?

```julia
verbose::Bool = false

```

?

---

<div class="post-metadata">

**Author:** ![Maysam\_Gholampour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maysam_gholampour/32/218184_2.png) [@Maysam\_Gholampour](https://discourse.julialang.org/u/Maysam_Gholampour)\
**Post date:** [February 27, 2024, 3:14am UTC](https://discourse.julialang.org/t/how-to-change-constant-variable-inside-a-package-for-testing/110739/7 "2024-02-27T03:14:51Z")

</div>

This is what I am not sure about the performance about the typed global variable. but this link can help to explain about global variable issue.

> <https://gist.github.com/flcong/2eba0189d7d3686ea9633a6d14398931>

To be honest, I have no idea about the performance of using global variable in package. We can define global paramters and wrap them into a function to improve the performance. I think we can consider the entire of package as a function.
