# 'UndefVarError: x not defined' when calling a macro outside of its module

**URL:** https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201
**Category:** New to Julia
**Created:** [January 29, 2019, 1:14am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201 "2019-01-29T01:14:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![FCo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fco/32/12808_2.png) [@FCo](https://discourse.julialang.org/u/FCo)
#### Post date: [January 29, 2019, 1:14am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/1 "2019-01-29T01:14:41Z")

</div>

I’m trying to code a macro which calls a function defined within the same module (with Julia v1.1). I always get the `ERROR: LoadError: UndefVarError: myModel not defined`.

The code of the module is the following (Models.jl):

```julia
module Models

export Model
export createmodel
export add_variable
export @add_variable

mutable struct Model
    variable_symbols::Dict{String, Symbol}
    variable_data::Dict{Symbol, Array{Int64, 1}}
    Model(; variable_symbols = Dict([]), variable_data = Dict([])) = 
        new(variable_symbols, variable_data)
    Model(variable_symbols, variable_data) = new(variable_symbols, 
        variable_data)
end

function createmodel()
    model = Model()
    return model
end

function add_variable(model, label::String, value::Array{Int64, 1})
    symbol = Symbol(label)
    model.variable_symbols[label] = Symbol(symbol)
    model.variable_data[symbol] = value
end

macro add_variable(model, var)
    quote
        add_variable($model, $(string(var)), $var)
    end
end

end

```

The macro is called as following:

```julia
include("Models.jl")
using .Models

x = [1, 2, 3]

myModel = createmodel()
@add_variable(myModel, x)
show(myModel)

```

I know that this is an escaping/hygiene problem, but after trying many ideas I don’t get the expected result. Up to now, my only way to get the expected result was to define the macro outside of the module to get: `Model(Dict("x"=>:x), Dict(:x=>[1, 2, 3]))`

---

<div class="post-metadata">

### Author: ![FCo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fco/32/12808_2.png) [@FCo](https://discourse.julialang.org/u/FCo)
#### Post date: [January 29, 2019, 1:28am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/2 "2019-01-29T01:28:19Z")

</div>

[SOLVED]

```julia
macro add_variable(model, var)
    quote
        add_variable($(esc(model)), $(string(var)), $(esc(var)))
    end
end

```

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [January 29, 2019, 1:28am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/3 "2019-01-29T01:28:27Z")

</div>

You’re right that this is an escaping problem. Here’s how to fix it:

```julia
macro add_variable_fixed(model, var)
    quote
        add_variable($(esc(model)), $(string(var)), $(esc(var)))
    end
end

```

Without the `esc`, julia will look for the bindings `myModel` and `var` inside the module `Models`. To see this in action, you can use `@macroexpand`:

```julia
julia> @macroexpand Models.@add_variable(myModel, x)
quote
    (Main.Models.add_variable)(Main.Models.myModel, "x", Main.Models.x)
end

julia> @macroexpand Models.@add_variable_fixed(myModel, x)
quote
    (Main.Models.add_variable)(myModel, "x", x)
end

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [January 29, 2019, 1:29am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/4 "2019-01-29T01:29:52Z")

</div>

Oh hey — welcome!

Try not to simultaneously cross post between Stack Overflow and here — lots of us browse both places and it just ends up duplicating efforts of volunteers.

[https://stackoverflow.com/questions/54412400/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module-julia](https://stackoverflow.com/questions/54412400/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module-julia)

---

<div class="post-metadata">

### Author: ![FCo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fco/32/12808_2.png) [@FCo](https://discourse.julialang.org/u/FCo)
#### Post date: [January 29, 2019, 1:30am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/5 "2019-01-29T01:30:32Z")

</div>

Thank you so much for your help Chris! It was so simple…  
And all my apologies for the cross post.

---

<div class="post-metadata">

### Author: ![FCo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fco/32/12808_2.png) [@FCo](https://discourse.julialang.org/u/FCo)
#### Post date: [February 13, 2020, 7:50am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/6 "2020-02-13T07:50:13Z")

</div>

I would like to write a similar macro to add multiple variables. To add, say, `x1`, `x2` and `x3` in the model, instead of writing:

```julia
@add_variable(myModel, x1)
@add_variable(myModel, x2)
@add_variable(myModel, x3)

```

I would prefer:

```julia
@add_variables(myModel, x1, x2, x3)

```

Any idea of how such macro could be written?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 13, 2020, 8:01am UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/7 "2020-02-13T08:01:31Z")

</div>

The entrance point is something like

```julia
julia> macro add_variables(ex...)
       @show ex
       return nothing
       end
@add_variables (macro with 1 method)

julia> @add_variables(a, b, c)
ex = (:a, :b, :c)

```

---

<div class="post-metadata">

### Author: ![FCo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fco/32/12808_2.png) [@FCo](https://discourse.julialang.org/u/FCo)
#### Post date: [February 13, 2020, 8:57pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-when-calling-a-macro-outside-of-its-module/20201/8 "2020-02-13T20:57:47Z")

</div>

[SOLVED]

Thanks for the hint, this is the way I write it:

```julia
macro add_variables(model, vars...)
    esc(quote
        for var in $vars
            add_variable($model, string(var), eval(var))
        end
    end)
end

```
