-
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
Unclear or incorrect arithmetic update-assignment documentation #2407
Comments
$ jq -nc '[[0,[1]],[2]] | .[0] += .[1]'
[[0,[1],2],[2]]
$ jq -nc '[[0,[1]],[2]] | .[0] |= . + .[1]'
[[0,[1],1],[2]] Yeah, |
I noticed now that there is at least one |
It will probably be a long while before the official Q: What is the difference between A: There are three types of update-assignment operators:
In the following, we will use += as the examplar or holotype of the The update-assignment operators operate on one or more JSON values as
for the different case of "op=". In a nutshell: (0) V | (E = F) is identical to V | (E = (V|F)) (1) V | (E |= F) is identical to V | (E = (V|E|F)) (2) V | (E += F) is identical to V | (E = (V|E) + (V|F)) The caveat is that when evaluating the expression
Examples: (0) {"a":1, "b":2} | .a = .b #=> {"a": 2, "b": 2} (1) {"a": {"b":"B"}} | .a |= .b #=> {"a": "B"} (2) {"a": 1, "b": 2} | .a += .b #=> {"a": 3, "b": 2} Executable tests:
|
@pkoppstein Yes would be a good additions i think. I will probably send PR to update to manual also unless someone does it before me. Hopefully it will get merged at some point. As some trivia I also noticed for
|
@wader - It seems to me that the behavior of //= that you describe is incorrect and should be fixed, and that it should not be documented except as a bug. |
Yes i was not expecting it to evaluate RHS and after thinking a bit more in regards to jq having functions with side effects i agree it feels like a bug. Would it be resonable to use @itchyny gojq seems to have the same behavior, what do you think? |
I didn't aware until now but that behavior looks buggy for sure, it should not evaluate RHS just like null coalescing operators in other languages, like C#, JavaScript, and Perl. Note that jq is kind a functional language with purity (with a few exceptions as you would know), and it sometimes has non-standard evaluation order which many people don't notice. jq evaluates RHS first in LHS + RHS but most people don't aware of this. |
Extracted the |
@itchyny I didn't know about the |
Hi
I noticed something when i tried to use the current value for
+=
. In the documentation it says:But it seems like for
a op= b
the input for the RHS is the input of the LHS and not. | a
.In my case I was trying to do something similar to this. Add something to a list conditionally on the current list value:
Here
length
will get{a:[1,2,3]}
as input not[1,2,3]
and return1
. But if i use|=
i get what i wanted:gojq seems to have the same behaviour which makes me think i might misunderstanding something? if so I can try to improve the documentation. If it's a bug it feels it might be a bit too late to change the behaviour?
The text was updated successfully, but these errors were encountered: