# How to replace macro argument with expr as being typed manually?

**URL:** https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630
**Category:** New to Julia
**Tags:** question
**Created:** [July 22, 2019, 1:56pm UTC](https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630 "2019-07-22T13:56:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Islam0mar](https://avatars.discourse-cdn.com/v4/letter/i/5daacb/32.png) [@Islam0mar](https://discourse.julialang.org/u/Islam0mar)
#### Post date: [July 22, 2019, 1:56pm UTC](https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630/1 "2019-07-22T13:56:30Z")

</div>

Given a macro `@NLobjective` which have the following usage example: `@NLobjective(m, Min, 11 x² - 484 x + 12358)`  
I want to write a function that returns the last expression `11 x² - 484 x + 12358`.  
Function implementation:

```julia
function CreateEq(x::VariableRef, arr::Array)
    s = 0
    for i in arr
        s += (x-i)^2
    end
    return :(@NLobjective(m, Min, begin $(s) end))
end

```

given x and `S = [5,6,7,9,11,16,20,22,23,24,99]`

Output from `eval(CreateEq(x,S))` is understood in a different way that writing `@NLobjective(m, Min, 11 x² - 484 x + 12358)` and gives the following error message:

> ERROR: Unexpected quadratic expression 11 x² - 484 x + 12358 in nonlinear expression. Quadratic expressions (e.g., created using @expression) and nonlinear expressions cannot be mixed.

Questions:

- How to make `begin $(s) end` be substituted as manually writing `11 x² - 484 x + 12358`?

---

<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: [July 22, 2019, 2:42pm UTC](https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630/2 "2019-07-22T14:42:50Z")

</div>

What are you trying to achieve? There is probably a better way to do it.

Do you want something like?

```nohighlight
function create_eq(model, x, arr)
    return @expression(model, sum((x - i)^2 for i in arr))
end

```

---

<div class="post-metadata">

### Author: ![Islam0mar](https://avatars.discourse-cdn.com/v4/letter/i/5daacb/32.png) [@Islam0mar](https://discourse.julialang.org/u/Islam0mar)
#### Post date: [July 22, 2019, 2:48pm UTC](https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630/3 "2019-07-22T14:48:20Z")

</div>

I want to get around this setup:

```julia
 my_expr = CreateEq(x,S)
 @variable(m, aux)
 @constraint(m, aux == my_expr)

```

I have tried your function using this: `@NLobjective(m, Min, @eval(create_eq(m,x,S)))`, but it gives the same error

---

<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: [July 22, 2019, 3:41pm UTC](https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630/4 "2019-07-22T15:41:08Z")

</div>

So I assume you want to include an affine or quadratic expression within a nonlinear objective? And you followed the work-around in the docs? [Nonlinear Modeling · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.2/nlp/#Syntax-notes-1)

At present, the work-around is recommended:

```nohighlight
function create_eq(model, x, arr)
    aux = @variable(model)
    @constraint(model, aux == sum((x - i)^2 for i in arr))
    return aux
end

model = Model()
@variable(model, x)
ex = create_eq(model, x, [1, 2, 3])
@NLobjective(model, Min, ex)

```

Also, please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757), and provide a minimal working example.

---

<div class="post-metadata">

### Author: ![Islam0mar](https://avatars.discourse-cdn.com/v4/letter/i/5daacb/32.png) [@Islam0mar](https://discourse.julialang.org/u/Islam0mar)
#### Post date: [July 23, 2019, 3:59am UTC](https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630/5 "2019-07-23T03:59:59Z")

</div>

Yes, that is it.

Thanks I will use your solution.

---

<div class="post-metadata">

### Author: ![Islam0mar](https://avatars.discourse-cdn.com/v4/letter/i/5daacb/32.png) [@Islam0mar](https://discourse.julialang.org/u/Islam0mar)
#### Post date: [July 24, 2019, 4:20am UTC](https://discourse.julialang.org/t/how-to-replace-macro-argument-with-expr-as-being-typed-manually/26630/6 "2019-07-24T04:20:22Z")

</div>

Here is the solution:

```nohighlight
function CreateEq(x::VariableRef, arr::Array)
    s = Expr(:call,:+)
    for i in arr
        push!(s.args, :((x - $i)^2))
    end
    return s
end

```

call it like that for macros:

```julia
 eval(:(@NLobjective(m, Min, $(CreateEq(x,S)))))

```
