There could be better error messages for @constraints macro

I believe it would be good to add to the error messages for the @constraints macro, to draw attention to the fact that a valid input for @constraint is not a valid input for @constraints. This is a mistake that is easy to make and hard to spot, but could be remedied by changing the error message or what constitutes a valid input for @constraints.

The @constraints macro allows one to write out multiple lines of contraints in a block, and returns a tuple of the constraints. This is great and I like using it as it aids readability.

Unfortunately, if one tries to treat @constraints like @constraint (without the s), Julia returns an unhelpful error.

For example: running this code gives an error. ERROR: LoadError: Invalid syntax for @constraints

using JuMP, GLPK 

# Preparing an optimization model
model = Model(GLPK.Optimizer)

# Declaring variables
@variable(model, x >= 0)

# Setting the objective
@objective(model, Min, x)

@constraints(model, 2x <= 1)

# Printing the prepared optimization model
print(model)

It is easy to disregard the “s” at the end of the constraints macro, and when doing so the error message given is not particularly helpful.

If you try editing the above code to use @constraint instead of @constraints, it runs correctly.

I am a newcomer to this language so I am hesitant to start by making a pull request. Let me know if this seems like a fair change to make.

1 Like

Thanks for the suggestion.

I’ve opened an issue and I will fix this today:

(The pull request could be a bit confusing for newcomers because it’s in the macro part of JuMP’s code.)

I am a newcomer to this language

Let me know if you have any other suggestions! I often overlook what parts are confusing to new users.

1 Like

One more suggestion, when using JuMP, it does not have code hints for the variables I define, which may cause me to forget some of the variables I have defined when I define many of them. If code hints are available, this problem can be solved. However, variables are defined by macros, and I do not know if there is a solution to this problem.

I don’t understand the suggestion? What is a code hint?

Sorry to make you confused, let me give an example to explain:

nNum = 10
@variable(model, test[1:nNum] >= 0)

I define a variable test and a parameter nNum above, when I add constraints below and I don’t get prompted for the variable I defined called ‘test’, but I do get prompted for the parameter called ‘nNum’. For example, when I type in ‘nN’, ‘nNum’ gets prompted, but ‘test’ does not unless I type in the full characters.

Is this in VS-Code? I can reproduce for test, but I can’t reproduce in general:

image

image

I think the issue is that test is too common, to the heuristic for what names to show doesn’t kick in? Regardless, this seems like an issue in VS-Code, not in JuMP.

Wow…But actually it doesn’t work on my PC, is there any other extensions you installed? except for intelliCode.

I have the https://www.julia-vscode.org. I don’t have intelliCode.