# There could be better error messages for @constraints macro

**URL:** <https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 27, 2023, 11:19pm UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293 "2023-02-27T23:19:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charlie\_Ruppe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charlie_ruppe/32/47280_2.png) [@Charlie\_Ruppe](https://discourse.julialang.org/u/Charlie_Ruppe)\
**Post date:** [February 27, 2023, 11:19pm UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/1 "2023-02-27T23:19:40Z")

</div>

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`

```julia
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.

---

<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:** [February 27, 2023, 11:44pm UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/2 "2023-02-27T23:44:16Z")

</div>

Thanks for the suggestion.

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

- [Improve error if block not provided to plural constraint · Issue #3254 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/3254).
- [Improve error message when plural macro not passed a block by odow · Pull Request #3255 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/pull/3255)

(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.

---

<div class="post-metadata">

**Author:** ![kevinqiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevinqiao/32/44492_2.png) [@kevinqiao](https://discourse.julialang.org/u/kevinqiao)\
**Post date:** [February 28, 2023, 10:00am UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/3 "2023-02-28T10:00:53Z")

</div>

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.

---

<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:** [February 28, 2023, 2:54pm UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/4 "2023-02-28T14:54:57Z")

</div>

> [@kevinqiao](#):
>
> it does not have code hints for the variables

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

---

<div class="post-metadata">

**Author:** ![kevinqiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevinqiao/32/44492_2.png) [@kevinqiao](https://discourse.julialang.org/u/kevinqiao)\
**Post date:** [March 1, 2023, 1:41am UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/5 "2023-03-01T01:41:44Z")

</div>

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

```julia
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.

---

<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:** [March 1, 2023, 1:57am UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/6 "2023-03-01T01:57:00Z")

</div>

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

![image](https://global.discourse-cdn.com/julialang/original/3X/e/b/eb29de36794f038faf9aa4a521994d574468b962.png)

![image](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4bfabce2827fefe430702f58b2f94312140d6fb.png)

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.

---

<div class="post-metadata">

**Author:** ![kevinqiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevinqiao/32/44492_2.png) [@kevinqiao](https://discourse.julialang.org/u/kevinqiao)\
**Post date:** [March 1, 2023, 2:29am UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/7 "2023-03-01T02:29:18Z")

</div>

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

---

<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:** [March 1, 2023, 2:43am UTC](https://discourse.julialang.org/t/there-could-be-better-error-messages-for-constraints-macro/95293/8 "2023-03-01T02:43:21Z")

</div>

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