# Confusing julia behavior. @everywhere macro changes the scope of local variables to global

**URL:** <https://discourse.julialang.org/t/confusing-julia-behavior-everywhere-macro-changes-the-scope-of-local-variables-to-global/77436>\
**Category:** General Usage\
**Tags:** parallel, distributed, scope\
**Created:** [March 5, 2022, 5:38am UTC](https://discourse.julialang.org/t/confusing-julia-behavior-everywhere-macro-changes-the-scope-of-local-variables-to-global/77436 "2022-03-05T05:38:56Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 6, 2022, 7:18pm UTC](https://discourse.julialang.org/t/confusing-julia-behavior-everywhere-macro-changes-the-scope-of-local-variables-to-global/77436/6 "2022-03-06T19:18:03Z")

</div>

I find this a bit confusing as well. One thing that often adds clarity is to replace macros with the code executed. In this case:

```julia
julia> using MacroTools

julia> prettify(@macroexpand(@fetchfrom 2 x))
:(Distributed.remotecall_fetch((()->x), 2))

```

so `@fetchfrom 2 x` actually means

```julia
remotecall_fetch(() -> x, 2)

```

i.e. it creates a closure around `x`, and then executes the result on pid 2. I can see how that ends up returning 1 given the closure is created on pid 1 and therefore likely closes over the local `x`, but I’m not sure why it changes the value of `x` on pid 2.

The way to get the value of `x` from other processes is to directly ask for the symbol defined in `Main` on those processes, as described in [this](https://stackoverflow.com/questions/27677399/julia-how-to-copy-data-to-another-processor-in-julia?noredirect=1&lq=1) SO answer:

```julia
julia> @fetchfrom 3 getfield(Main, :x)
3

julia> @everywhere println(x)
1
      From worker 2:	2
      From worker 3:	3

```

---

_[View the full topic](https://discourse.julialang.org/t/confusing-julia-behavior-everywhere-macro-changes-the-scope-of-local-variables-to-global/77436)._
