# How can i change variables of modules from other files or different modules?

**URL:** <https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 29, 2018, 12:30pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661 "2018-09-29T12:30:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 12:30pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/1 "2018-09-29T12:30:36Z")

</div>

Let’s say I got something like this:

```julia
module a
 global test = "hi"
end

# Another file
module b
 import a
 a.test = "bye" # ErrorException("cannot assign variables in other modules")
end

```

Looks like it’s impossible to change **GLOBAL** variables or variables in general from other modules which is very weird in my opinion, are there any alternative to this?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 29, 2018, 12:42pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/2 "2018-09-29T12:42:43Z")

</div>

`eval`

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 12:43pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/3 "2018-09-29T12:43:39Z")

</div>

Thank you, like this?

```julia
module a
 global test = "hi"
end

# Another file
module b
 import a
 @eval a.test = "bye" # ErrorException("cannot assign variables in other modules")
end

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 29, 2018, 12:53pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/4 "2018-09-29T12:53:08Z")

</div>

There may be another way using `eval`, but here are 2 ways:

```julia
julia> module a
        global test = Ref("hi")
       end
Main.a

julia> module b
        import Main.a
        a.test[] = "bye"
       end
Main.b

julia> a.test
Base.RefValue{String}("bye")

julia> module a
        global test = "hi"
        settest(t) = (global test = t)
       end
WARNING: replacing module a.
Main.a

julia> module b
        import Main.a
        a.settest("bye")
       end
WARNING: replacing module b.
Main.b

julia> a.test
"bye"

```

You can ignore the `Main.` if they are real packages.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 12:57pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/5 "2018-09-29T12:57:29Z")

</div>

Thank you very much, I got a question about the first way, what if I want to change from String to Dict? it will automatically error:  
`ERROR: MethodError: Cannot convert an object of type Dict{String,String} to an object of type String`

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 29, 2018, 1:00pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/6 "2018-09-29T13:00:36Z")

</div>

> [@TheOnlyArtz](#):
>
> it will automatically error:

Yes and this is probably a good thing! In Julia variables taking multiple types are frowned upon. You can do `global test = Base.RefValue{Union{String, Dict{String, String}}}("hi")`, but that will reduce the performance of your code. Also it is better if you make it a constant.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 29, 2018, 1:00pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/7 "2018-09-29T13:00:50Z")

</div>

> [@TheOnlyArtz](#):
>
> @eval a.test = “bye” # ErrorException(“cannot assign variables in other modules”)

… `@eval a test = "bye"`

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 1:01pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/8 "2018-09-29T13:01:35Z")

</div>

> Also it is better if you make it a constant.

What do you mean by that?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 29, 2018, 1:03pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/9 "2018-09-29T13:03:36Z")

</div>

```julia
julia> const test = Base.RefValue{Union{String, Dict{String, String}}}("hi")
Base.RefValue{Union{Dict{String,String}, String}}("hi")

julia> test[] = "bye"
"bye"

julia> test = "bye"
ERROR: invalid redefinition of constant test
Stacktrace:
 [1] top-level scope at none:0

```

`test` can be a constant binding to a mutable object. So you can mutate the object it is bound to, but you cannot rebind it to another object. The second command is valid, the third is not, even if you try to bind it to another `RefValue` of the same type.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [September 29, 2018, 1:04pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/10 "2018-09-29T13:04:36Z")

</div>

Oh alright so I guess the second way is valid and performs way better too, right?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 29, 2018, 1:05pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/11 "2018-09-29T13:05:06Z")

</div>

> [@TheOnlyArtz](#):
>
> Oh alright so I guess the second way is valid and performs way better too, right?

Yes AFAIU, the constant Ref method I assume you mean.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 29, 2018, 1:18pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/12 "2018-09-29T13:18:01Z")

</div>

> [@TheOnlyArtz](#):
>
> Oh alright so I guess the second way is valid and performs way better too, right?

No. If you can accept solution that require changes of module `a`, than using a const global ref is way better. This is especially true if the type is known.

---

<div class="post-metadata">

**Author:** ![PurgePJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purgepj/32/5566_2.png) [@PurgePJ](https://discourse.julialang.org/u/PurgePJ)\
**Post date:** [September 29, 2018, 1:29pm UTC](https://discourse.julialang.org/t/how-can-i-change-variables-of-modules-from-other-files-or-different-modules/15661/13 "2018-09-29T13:29:38Z")

</div>

Second method worked, thanks.
