# Revise and const values changed during a session?

**URL:** <https://discourse.julialang.org/t/revise-and-const-values-changed-during-a-session/64372>\
**Category:** General Usage\
**Created:** [July 9, 2021, 6:18pm UTC](https://discourse.julialang.org/t/revise-and-const-values-changed-during-a-session/64372 "2021-07-09T18:18:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sapo](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sapo](https://discourse.julialang.org/u/sapo)\
**Post date:** [July 9, 2021, 6:18pm UTC](https://discourse.julialang.org/t/revise-and-const-values-changed-during-a-session/64372/1 "2021-07-09T18:18:23Z")

</div>

Until now, I was used to having a file containing a module with some parameters, usually set with `const` keyword. This is very similar to a [singleton](https://discourse.julialang.org/t/implementing-singletons-for-configuration-variables) - the design pattern, not the Julian singleton - and guarantees that anywhere in the code the configuration variables that I am using are the same.

During an interactive session, I just did `include("src/myincludes.jl"); MyModule.main()` to run the code while testing various parameters.

Today, I have some [issues](https://github.com/JuliaIO/JLD.jl/issues/54) with JLD and type identity, for which I need to reload some file. For this reason, I am now using Revise. However, Revise I cannot reproduce the same workflow.

For instance, suppose having the following 4 files:

```julia
# A.jl
module A

const a = 6
b = 6

end

```

```julia
# B.jl
module B
using Main.A: a, b
function test()
    println(a, b)
end
end

```

```julia
# C.jl
module C
using Main.B: test
function main()
    test()
end
end

```

```julia
# D.jl
using Revise
__revise_mode__ = :eval

includet("A.jl")
includet("B.jl")
includet("C.jl")

```

During an interactive session, I run:

```julia
julia> include("D.jl");

julia> B.test()
66

julia> C.main()
66

```

Then, I change the values in `A.jl` and retry, but the output is still the same

```julia
julia> include("D.jl");

julia> B.test()
66

julia> C.main()
66

```

What is the most similar workflow to the one I am used while using Revise? I really need to have a module with many const values in it and to change those values in a running session.

---

<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:** [July 9, 2021, 9:31pm UTC](https://discourse.julialang.org/t/revise-and-const-values-changed-during-a-session/64372/2 "2021-07-09T21:31:34Z")

</div>

I don’t know a way to do what you want without any syntax changes, but if you don’t mind changing how you acess the variables a little, you can do

```julia
module A
a() = 6
end

```

and access it via `a()` (i.e. define a function and call it)  
or

```julia
module A
const a = Ref(6)
end

```

and access it by `a[]`.

These of course are different things; by defining a function, you can rely on Julia to invalidate old code when the value changes and recompile (and Revise works well with this). When you mutate a ref, `a[] = 1`, then of course any code accessing the ref gets the current value. Revise does not work well with this approach if you modify the source code to e.g. `const a = Ref(1)` but you can modify it from code like `a[] = 1`.

---

<div class="post-metadata">

**Author:** ![sapo](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sapo](https://discourse.julialang.org/u/sapo)\
**Post date:** [July 12, 2021, 8:38am UTC](https://discourse.julialang.org/t/revise-and-const-values-changed-during-a-session/64372/3 "2021-07-12T08:38:03Z")

</div>

Thank you so much! Today I have prepared a little test comparing your solution and another idea I had involving mutable structs. Your solution is by far the best (even better the not-working one).

Here the code and tests:

```julia
# A.jl
module A

# METHOD 1
# the following works but are not re-evaluated by Revise
const a = 5
b = 5

# METHOD 2
# functions are re-evaluated by Revise when they are changed
const c() = 5
d() = 5

# METHOD 3
mutable struct S
    vars::NamedTuple
    function S()
        # the following ensures that S cannot be reinstantiated without explicit
        # usage of `eval`
        isdefined(S, :s) && error("Cannot instantiate S twice")
        return new()
    end
end

# `const` ensures that the reference to the object will always be the same
const s = S()

# an easier way to get the settings
function getS()
    return s.vars
end

# defining fields in a function allows Revise to re-evaluate it when settings
# are changed
function init()
    s.vars = (a = 5, b = 5)
end

end

```

```julia
# B.jl
module B
using Main.A: a, b, c, d, getS
function test1()
    r = 0
    for i in 1:1000
        r += a + b
    end
    return r
end
function test2()
    r = 0
    for i in 1:1000
        r += c() + d()
    end
    return r
end
function test3()
    s = getS()
    r = 0
    for i in 1:1000
        r += s.a + s.b
    end
    return r
end
end

```

```julia
# C.jl
module C
using BenchmarkTools
using Main.B: test1, test2, test3
using Main.A: init

function main()
    init()
    println(@btime test1())
    println(@btime test2())
    println(@btime test3())
end
end

```

```julia
# D.jl
using Revise

includet("A.jl")
includet("B.jl")
includet("C.jl")

```

And here the tests:

```julia
julia> include("D.jl");

julia> C.main()
k 30.400 μs (958 allocations: 14.97 KiB)
12000
  1.492 ns (0 allocations: 0 bytes)
12000
  62.388 μs (958 allocations: 14.97 KiB)
12000

julia> C.main() # re-run after having changed the settings
  30.289 μs (958 allocations: 14.97 KiB)
12000
  1.492 ns (0 allocations: 0 bytes)
10000
  62.629 μs (949 allocations: 14.83 KiB)
10000

```

As you see, method 1 doesn’t work on re-evaluation, method 3 works but is 2x slower, while method 2 works and is 20.000x faster
