-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable assignment with arithmetic (Operator precedence) #1928
Comments
Yes, jq has some precedence issues. I don't think they can be fixed now. Perhaps we could add higher- and lower-precendence versions of various operators, not unlike Haskell, but that's about all we can do. I.e., something like Not sure that it's worth doing. Comments? |
I think it's something that would be useful eventually to get a more intuitive behavior. I'm not super familiar with the underlying code, so I don't know how much work it would entail. Since I am able to work around the current limitations by adding some |
I don't like the idea of having otherwise-identical operators with different precedence semantics. I think that's likely to be more confusing long-term. (See the difference between In a world with a formal compile step, I might consider adding a warning (similar to what you get when you do That said, I wouldn't want to add warning output like that here. |
I was imagining it differently than how you have described it @wtlangford. I see So something more complicated like
Not sure if all of that fits in with the current implementation, but that fleshes out my thoughts a little more. |
Describe the bug
Arithmetic operations combined with variable assignment leads to unintuitive parsing.
To Reproduce
https://jqplay.org/s/EYE60uy1jx
Input
JQ
Output
The value of
$array_length
is1
, instead of the expected4
Expected behavior
The grouping during parsing seems unintuitive and/or incorrect here.
What the current implementation is doing:
What I would expect:
Environment (please complete the following information):
Additional context
This is the simplest example I could create, but in more complicated examples, this creates some unintuitive results:
I ended up here while trying to figure out why something like this example wasn't working:
https://jqplay.org/s/EHrQ49_MDl
Input:
JQ:
Result:
I understand I can get around this issue by explicitly adding some parentheses to force the grouping in the way I am wanting, or by removing the
$new
variable declaration and just in-lining it in the object constructor. These are just examples to show the behavior I was dealing with.The text was updated successfully, but these errors were encountered: