# Error escaping a variable in a module using JuMP

**URL:** <https://discourse.julialang.org/t/error-escaping-a-variable-in-a-module-using-jump/89998>\
**Category:** Optimization (Mathematical)\
**Created:** [November 9, 2022, 5:35pm UTC](https://discourse.julialang.org/t/error-escaping-a-variable-in-a-module-using-jump/89998 "2022-11-09T17:35:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [November 9, 2022, 5:35pm UTC](https://discourse.julialang.org/t/error-escaping-a-variable-in-a-module-using-jump/89998/1 "2022-11-09T17:35:53Z")

</div>

I’m working to update a module compatible with JuMP v1.1.1 to the newest version of JuMP. Within the module is code similar to:

```julia
module HoldBug

using JuMP

export @mytest

macro mytest(m,F)
    m = esc(m)
    x = esc(F)
    :(@NLconstraint( $(m), $(x) <= 4) )
end

end # module HoldBug

using .HoldBug
using JuMP

m = Model()

@variable(m,x)

@macroexpand @mytest(m,x)

@mytest(m,x)

```

In the newest version of JuMP I get

```julia
UndefVarError: x not defined. 

```

Expanding the macro reveals the issue, in JuMP1.4 the variable x is being held by HoldBug

```julia
var"#4###315" = JuMP.Expr(:escape, Main.HoldBug.x)

```

In JuMP1.1, it looks like this

```julia
JuMP._parse_NL_expr_runtime(m, x, var"#5#tape", var"#7###315", var"#6#values")

```

Does anybody have advice on how to work around this?

Thanks,

Mitch

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 9, 2022, 6:57pm UTC](https://discourse.julialang.org/t/error-escaping-a-variable-in-a-module-using-jump/89998/2 "2022-11-09T18:57:55Z")

</div>

Writing macros that call macros is very difficult to get correct. You need to properly escape things.

```julia
julia> module HoldBug
       using JuMP
       macro mytest(m, F)
           return esc(:(@NLconstraint($m, $F <= 4)))
       end
       end # module HoldBug
WARNING: replacing module HoldBug.
Main.HoldBug

julia> using .HoldBug

julia> using JuMP

julia> m = Model();

julia> @variable(m, x);

julia> @macroexpand HoldBug.@mytest(m, x)
quote
    #= REPL[175]:4 =#
    JuMP._valid_model(m, :m)
    begin
        #= /Users/oscar/.julia/dev/JuMP/src/macros.jl:2383 =#
        JuMP._init_NLP(m)
        #= /Users/oscar/.julia/dev/JuMP/src/macros.jl:2384 =#
        begin
            #= /Users/oscar/.julia/dev/JuMP/src/macros.jl:2377 =#
            begin
                #= /Users/oscar/.julia/dev/JuMP/src/macros.jl:1846 =#
                JuMP._init_NLP(m)
                var"#73###323" = JuMP.Expr(:call, :<=, x, 4)
            end
            #= /Users/oscar/.julia/dev/JuMP/src/macros.jl:2378 =#
            JuMP.add_nonlinear_constraint(m, var"#73###323")
        end
    end
end

julia> HoldBug.@mytest(m, x)
x - 4.0 ≤ 0

```

What are you trying to achieve? You should consider instead the raw expression input: [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/#Raw-expression-input)

---

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [November 9, 2022, 7:28pm UTC](https://discourse.julialang.org/t/error-escaping-a-variable-in-a-module-using-jump/89998/3 "2022-11-09T19:28:33Z")

</div>

I’m updating the Complementarity package, this piece of code is from the MPEC portion of the module.

Right now the package recursively escapes the expression F, so for example if F = 3\*x-y then inside of the macro you get something like

```julia
3*esc(x) - esc(y)

```

rather than

```julia
esc(3*x-y)

```

I’ll experiment with add\_nonlinear\_constraint, but I’ve tried this in the past and it seems to be a similar issue, where the module hangs onto the variable.

Thanks for taking a look at it

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 9, 2022, 9:02pm UTC](https://discourse.julialang.org/t/error-escaping-a-variable-in-a-module-using-jump/89998/4 "2022-11-09T21:02:55Z")

</div>

> [@Mitch\_Phillipson](#):
>
> I’m updating the Complementarity package

Ah. This is pretty non-trivial. We changed quite a lot. The complementarity package is also really old. It’s almost worth using it as inspiration for a rewrite, rather than trying to minimally modify the code.

---

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [November 9, 2022, 9:34pm UTC](https://discourse.julialang.org/t/error-escaping-a-variable-in-a-module-using-jump/89998/5 "2022-11-09T21:34:47Z")

</div>

Good to know. This is actually the last piece to have the code, at least, run. It’s also a good exercise to learn more Julia.

Thanks again for the advice.
