# How to intercept creation or setting of variable in current module?

**URL:** <https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034>\
**Category:** General Usage\
**Created:** [April 3, 2023, 9:49pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034 "2023-04-03T21:49:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [April 3, 2023, 9:49pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/1 "2023-04-03T21:49:36Z")

</div>

Need help, how to intercept operation on create new variable on current module in Julia 1.8?

For example, to intercept `MyModule.x = 1` in Julia 1.8 i do this:

```julia
julia> module MyModule end;

julia> function Base.setproperty!(m::Module, p::Symbol, v)
           println("variable $p changed on $v in module $m")
           @eval m $p = $v # waiting for julia 1.9 and will use setglobal!()
       end;

julia> MyModule.x = 1;
variable x changed on 1 in module Main.MyModule

```

How to intercept `x = 1` in `Main`?

```julia
julia> Main.x = 1;
variable x changed on 1 in module Main

julia> x = 1; # must print like Main.x = 1

```

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [April 4, 2023, 12:18pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/2 "2023-04-04T12:18:03Z")

</div>

Any ideas?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [April 4, 2023, 1:58pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/3 "2023-04-04T13:58:27Z")

</div>

So you want to circumvent the restriction / error “cannot assign variables in other modules”?

Could you explain what you are trying to accomplish with this?

`MyModule.x` would be a global; so it should presumably be a `const`.

If you want specific variables to be assignable from the outside of `MyModule`, you could just write a function (in `MyModule`) to assign those values.  
Do you really want to permit creation of arbitrary globals (with possible side effects)?

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [April 4, 2023, 2:15pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/4 "2023-04-04T14:15:04Z")

</div>

I must create a trigger on change or add the variable in the current module.

When a variable is created or modified in the current module, my function must be called.

This must happen regardless of how the module variable is created or modified. When `julia> x=1` in Main too.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [April 4, 2023, 4:20pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/5 "2023-04-04T16:20:44Z")

</div>

It would help to get a bigger picture idea of what you are trying to accomplish.

If you just need to run a function when a variable in `MyModule` is modified, can’t you ensure that the variable is modified by calling a function (`MyModule.change_x())`? Then `change_x()` can call the function you need to be called.

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [April 4, 2023, 5:24pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/6 "2023-04-04T17:24:21Z")

</div>

I can’t.

Variables in Main or any other module are created in a independent way.

I only must to call foo() when a module variable is created or modified.

If the variable was created in Main from the REPL like this:

```julia
julia> x = 1;

```

i must call foo() too.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [April 4, 2023, 6:06pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/7 "2023-04-04T18:06:26Z")

</div>

Sorry - I don’t know how to accomplish this.

For curiosity - the pattern you are aiming for seems very unusual. Why do you need to create variables in a module from the REPL?

Perhaps the question to ask is: is this the right design choice?

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [April 4, 2023, 7:29pm UTC](https://discourse.julialang.org/t/how-to-intercept-creation-or-setting-of-variable-in-current-module/97034/8 "2023-04-04T19:29:45Z")

</div>

I am trying to visualise the workspace without refresh all module variables by Base.names().
