# Variable scoping in macros: Deprecated syntax \`implicit assignment to global variable\`

**URL:** <https://discourse.julialang.org/t/variable-scoping-in-macros-deprecated-syntax-implicit-assignment-to-global-variable/8131>\
**Category:** General Usage\
**Created:** [January 3, 2018, 2:58pm UTC](https://discourse.julialang.org/t/variable-scoping-in-macros-deprecated-syntax-implicit-assignment-to-global-variable/8131 "2018-01-03T14:58:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [January 3, 2018, 2:58pm UTC](https://discourse.julialang.org/t/variable-scoping-in-macros-deprecated-syntax-implicit-assignment-to-global-variable/8131/1 "2018-01-03T14:58:12Z")

</div>

I’m running into a deprecation when running the test/perf/micro benchmarks in 0.7-DEV. The issue is implicit declaration and assignment of global variables in the `@timeit` macro in test/perf/perfutils.jl. Here it is boiled down to a minimal working example:

```julia
ulia> macro timeit(ex)
           quote
               t = Float64[] # no problem!
               tot = 0.0 # deprecated! 
               i = 0 # deprecated! 
               while i < 5 || tot < 0.0001
                   e = 1000*(@elapsed $(esc(ex)))
                   tot += e
                   if i > 0
                       # warm up on first iteration
                       push!(t, e)
                   end
                   i += 1
               end
               @show tot, t
           end
       end
@timeit (macro with 1 method)

julia> expression = Meta.parse("x=2; y=3; x+y")
:($(Expr(:toplevel, :(x = 2), :(y = 3), :(x + y))))

julia> @timeit(expression)
┌ Warning: Deprecated syntax `implicit assignment to global variable `#2#tot``.
│ Use `global #2#tot` instead.
└ @ nothing none:0
┌ Warning: Deprecated syntax `implicit assignment to global variable `#3#i``.
│ Use `global #3#i` instead.
└ @ nothing none:0
(tot, t) = (0.00020499999999999997, [1.6e-5, 6.3e-5, 2.1e-5, 2.0e-5])
(0.00020499999999999997, [1.6e-5, 6.3e-5, 2.1e-5, 2.0e-5])

julia> versioninfo()
Julia Version 0.7.0-DEV.3234
Commit 2cc82d29e1* (2018-01-02 11:44 UTC)
Platform Info:
  OS: Linux (x86_64-suse-linux)
  CPU: Intel(R) Core(TM) i7-3960X CPU @ 3.30GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, sandybridge)
Environment:

```

Is there a good way to force a local scope inside the macro in order to suppress the warning? Or should this be accomplished by turning the macro into a function? Or is a macro necessary to avoid overhead in timing the evaluating the expression?

Any help appreciated.

---

<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 3, 2018, 3:01pm UTC](https://discourse.julialang.org/t/variable-scoping-in-macros-deprecated-syntax-implicit-assignment-to-global-variable/8131/2 "2018-01-03T15:01:27Z")

</div>

How about wrapping everything in a `let` block e.g.

```julia
quote
    let
        ...
    end
end

```

Ideally, the perf benchmarks should just be rewritten to use BenchmarkTools though.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [January 3, 2018, 5:33pm UTC](https://discourse.julialang.org/t/variable-scoping-in-macros-deprecated-syntax-implicit-assignment-to-global-variable/8131/3 "2018-01-03T17:33:43Z")

</div>

That does the job. Thank you. (I’d tried `begin ... end` and C-style enclosing braces. Still learning Julia basics…)

About updating to BenchmarkTools, should I take that as an order? 🙂 I wasn’t sure if the non-use of BenchmarkTools was just historical, or if it was to maximize transparency and similarity to timing algorithms in other languages.
