# @eval and module scope

**URL:** <https://discourse.julialang.org/t/eval-and-module-scope/6166>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [September 30, 2017, 12:28pm UTC](https://discourse.julialang.org/t/eval-and-module-scope/6166 "2017-09-30T12:28:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 30, 2017, 12:28pm UTC](https://discourse.julialang.org/t/eval-and-module-scope/6166/1 "2017-09-30T12:28:10Z")

</div>

(migrated from Slack)  
Consider this code

```julia
function define_ops(x)
       for sym in (:pos, :pes)
               @eval $(sym)(y) = y + $x
       end
end

define_ops(2)
pos(3) #5

```

Works perfectly

But if I try to do it in a module (I believe `eval` works in module scope?), it doesn’t:

```julia
module FooMod
function define_ops(x)
       for sym in (:pos, :pes)
               @eval $(sym)(y) = y + $x
       end
end
export define_ops
export pos, pes
end

using FooMod
define_ops(2)
pos(3) # 'pos not defined' error

```

clearly something about scoping I don’t understand. What idiom would I use to make the below version work?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 30, 2017, 12:37pm UTC](https://discourse.julialang.org/t/eval-and-module-scope/6166/2 "2017-09-30T12:37:34Z")

</div>

I think you might need to eval in `current_module()`:

```julia
eval(current_module(), ...)

```

(which might later be superseded by `@ __MODULE__ `)

```julia
help?> current_module
search: current_module

  current_module() -> Module

  Get the dynamically current Module, which is the Module code is currently being read from. In general, this is not the same as the module containing the call to this function.

```

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [September 30, 2017, 12:42pm UTC](https://discourse.julialang.org/t/eval-and-module-scope/6166/3 "2017-09-30T12:42:50Z")

</div>

Seems to work for me

```julia
julia> module Foo
           function define_ops(x)
              for sym in (:pos, :pes)
                      @eval $(sym)(y) = y + $x
              end
           end
           export define_ops
           export pos
       end
Foo

julia> using Foo

julia> define_ops(3)

julia> pos(4)
7

julia> versioninfo()
Julia Version 0.6.1-pre.1
Commit 4b967d4a9d (2017-08-21 21:11 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin15.6.0)
  CPU: Intel(R) Core(TM) M-5Y51 CPU @ 1.10GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, broadwell)

```

---

<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 30, 2017, 12:43pm UTC](https://discourse.julialang.org/t/eval-and-module-scope/6166/4 "2017-09-30T12:43:20Z")

</div>

No don’t `eval` in `current_module()`. It is also **NOT** equivalent to `@ __MODULE__ `

Works fine on master

```julia
julia> module FooMod
       function define_ops(x)
              for sym in (:pos, :pes)
                      @eval $(sym)(y) = y + $x
              end
       end
       export define_ops
       export pos, pes
       end
Main.FooMod

julia> using .FooMod

julia> define_ops(2)

julia> pos(1)
3

julia> pos(2)
4

julia> pes(2)
4

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 30, 2017, 1:15pm UTC](https://discourse.julialang.org/t/eval-and-module-scope/6166/5 "2017-09-30T13:15:58Z")

</div>

Wut? I’ll download a nightly and see if it’s a setup/version thing.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 1, 2017, 6:36am UTC](https://discourse.julialang.org/t/eval-and-module-scope/6166/6 "2017-10-01T06:36:01Z")

</div>

I’ve no idea what went wrong, but the exact pasted code above worked after restarting my session.
