title: Julia+JuMP: variable number of arguments to function
body: <p>I'm trying to use JuMP to solve a non-linear problem, where the number of variables are decided by the user - that is, not known at compile time.</p>
  
  <p>To accomplish this, the <code>@NLobjective</code> line looks like this:</p>
  
  <pre><code>@eval @JuMP.NLobjective(m, Min, $(Expr(:call, :myf, [Expr(:ref, :x, i) for i=1:n]...)))
  </code></pre>
  
  <p>Where, for instance, if <code>n=3</code>, the compiler interprets the line as identical to:</p>
  
  <pre><code>@JuMP.NLobjective(m, Min, myf(x[1], x[2], x[3]))
  </code></pre>
  
  <p>The issue is that <code>@eval</code> works only in the global scope, and when contained in a function, an error is thrown.</p>
  
  <p><strong>My question is:</strong> how can I accomplish this same functionality -- getting <code>@NLobjective</code> to call <code>myf</code> with a variable number of <code>x[1],...,x[n]</code> arguments -- within the local, not-known-at-compilation scope of a function?</p>
  
  <pre><code>def testme(n)
      myf(a...) = sum(collect(a).^2)
  
      m = JuMP.Model(solver=Ipopt.IpoptSolver())
  
      JuMP.register(m, :myf, n, myf, autodiff=true)
      @JuMP.variable(m, x[1:n] &gt;= 0.5)
  
      @eval @JuMP.NLobjective(m, Min, $(Expr(:call, :myf, [Expr(:ref, :x, i) for i=1:n]...)))
      JuMP.solve(m)
  end
  
  testme(3)
  </code></pre>
  
  <p>Thanks!</p>