Skip to content

Make plural macros (e.g. @variables) return collections of the defined objects #2837

@MartinBonde

Description

@MartinBonde

When working on a large model it is extremely useful to be able to manipulate large groups of variables or constraints.

To avoid repeating long lists of variable names or equations, it would be very useful if the the ‘plural macros’ (e.g. @variables and @Constraints), returned collections of the newly defined objects.

For example I would like for

vars = @variables model begin
  x
  y[1:2]
end

to be equivalent to writing

@variables model begin
  x
  y[1:2]
end
vars = [x, y]

and

eqs = @NLconstraints model begin
  E_x, x == 1
  E_y[i ∈ 1:2], y[i] == x
end

should be equivalent to writing

@NLconstraints model begin
  E_x, x == 1
  E_y[i ∈ 1:2], y[i] == x
end
eqs = [E_x, E_y]

Note that this is not just a matter of laziness, but that repeating long lists of items is prone to hard-to-debug errors. I.e. spotting which item is missing in a large collection of variables or constraints can take a long time.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions