# Passing constant configuration values to modules

**URL:** <https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551>\
**Category:** General Usage\
**Tags:** module\
**Created:** [September 9, 2019, 5:20am UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551 "2019-09-09T05:20:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [September 9, 2019, 5:20am UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/1 "2019-09-09T05:20:29Z")

</div>

I am working on a numerical optimisation project with Julia in a Jupyter notebook. My current workflow is to define the problem size with some global constants at the top of the notebook (eg. const N = 20), which are then used throughout my code.

In trying to organise my code better (and to avoid conflicts between variables in different files), I came across [this article](https://florianwilhelm.info/2018/11/working_efficiently_with_jupyter_lab/), and am now trying to move the majority of my code into a Julia module which would be imported by the notebook. I would still like to define my problem with global constants at the top of the notebook. **Is this possible, and is it a good idea?**

My instinct for passing these values into the module would be with an initialisation function, which could set global values in the module. However, I don’t believe this allows the values to be const, which I believe has performance impacts. An alternative would be to specify the constants directly in the module - would this be better? Is this necessary due to Julia’s precompilation of modules (which I have a limited understanding of)?

---

<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:** [September 9, 2019, 7:15am UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/2 "2019-09-09T07:15:45Z")

</div>

> [@aahaselgrove](#):
>
> I would still like to define my problem with global constants at the top of the notebook. **Is this possible, and is it a good idea?**

Well, it is possible but not really a good idea. Why not pass your problem properties to the optimization functions?

So instead of

```julia
N = 20
iterations = 10
savepath = "myfile"

solve()

```

you can do

```julia
solve(N = 20, iterations = 10, savepath = "myfile")

```

Or have a struct that holds the options:

```julia
opts = OptimizationOptions(N=20, iterations=10, savepath="myfile")
solve(opts)

```

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 9, 2019, 7:45am UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/4 "2019-09-09T07:45:38Z")

</div>

In such cases I tend to use [Parameters.jl](https://github.com/mauro3/Parameters.jl)

then I simply create some file `vars.jl` in which i define all the needed parameters and simply include it my project.

**vars.jl**

```julia
 using Parameters

julia> @with_kw struct A
           a::Int = 6
           b::Float64 = -1.1
           c::UInt8 = 42
       end

```

**main.jl**

```julia
include("./vars.jl")
B=A()

```

and then I simply access the values with `B.a` and so on. It also allows to simply modify the values using `C = A(a=7)`

---

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [September 9, 2019, 7:58am UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/5 "2019-09-09T07:58:45Z")

</div>

I have a bunch of functions which all use the same constants. In my previous MATLAB implementation, I had a constants struct which was passed through every function, but when I moved to Julia, it seemed neater to use global constants, in what I feel is a similar style to a C #define.

---

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [September 9, 2019, 8:01am UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/6 "2019-09-09T08:01:27Z")

</div>

The solution I am going with is to have a parameters submodule which contains and exports the constants to all other submodules which use them.

---

<div class="post-metadata">

**Author:** ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)\
**Post date:** [January 27, 2021, 6:17pm UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/7 "2021-01-27T18:17:03Z")

</div>

> [@kristoffer.carlsson](#):
>
> Well, it is possible but not really a good idea.

I hope you don’t mind if I necro this thread: How is it possible?

I have a similar issue but with a different application.

We have a project that analyzes a large number of different data sets, with many steps of analysis. We have many functions that primarily exist to organize sections of the data analysis (though a few are reused frequently, most are used only once). We are organizing different scripts into modules to reduce risks of namespace collision and to further clarify ideas (when there are a very large number of steps involved in cleaning multiple data sets, long scripts that aren’t broken down into functions and further into modules can be very difficult to read, and are susceptible to hard-to-debug mistakes caused by state (for example, someone might define df = CSV.read(“path”, DataFrame) in one part of a script, then a different dataframe is loaded subsequently,. If the second loading line is accidentally deleted, then we have a difficult bug to detect or find… very bad)).

We have a config.json file which we use to load global configuration parameters, like directory paths. We had defined a global const CONFIG = dictionary\_from\_JSON\_file.

yes, in principle we could add an additional filepath arguments to every function that might call a function that saves a figure or loads some data, but this clutters up the code and increases, rather than decreases, the risks of a mistake. Alternatively, we reload the global CONFIG constant from the JSON file into each module, but this seems repetitive. Another possibility is to eschew modules altogether.

How does one pass global constants into a module?

I suppose a fourth alternative is to use a "let CONFIG=CONFIG " block

---

<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:** [January 27, 2021, 8:30pm UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/8 "2021-01-27T20:30:08Z")

</div>

> [@croberts](#):
>
> How does one pass global constants into a module?

You mean like:

```julia
julia> const CONFIG = 1
1

julia> module M
       import ..CONFIG

       run_analysis() = print("running with config = $CONFIG")
       end
Main.M

julia> M.run_analysis()
running with config = 1

```

---

<div class="post-metadata">

**Author:** ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)\
**Post date:** [January 28, 2021, 3:33pm UTC](https://discourse.julialang.org/t/passing-constant-configuration-values-to-modules/28551/9 "2021-01-28T15:33:25Z")

</div>

I had no idea import worked on consts, thank you so much!
