-
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
Update-assignment //= always evaluates right-hand-side #2410
Comments
I think the following definition will fix the problem def _update_alt(ps; f): . as $x | ps |= (. // ($x | f));
But it breaks some cases. $ jq -nc '.a //= empty'
$ jq -nc '_update_alt(.a; empty)'
null $ jq -nc '.a //= range(3)'
{"a":0}
{"a":1}
{"a":2}
$ jq -nc '_update_alt(.a; range(3))'
{"a":0} |
Trying to wrap my head around this. The breaking seems to happen because This feels a bit unintuitive:
|
The behavior differs between |
Aha yeah i noticed the code for _modify is quite special also |
@wader let me help you wrap your mind around this: Exp "//=" Exp {
$$ = gen_definedor_assign($1, $3);
} | static block gen_definedor_assign(block object, block val) {
block tmp = gen_op_var_fresh(STOREV, "tmp");
return BLOCK(gen_op_simple(DUP),
/*here -->*/ val, tmp,
gen_call("_modify", BLOCK(gen_lambda(object),
gen_lambda(gen_definedor(gen_noop(),
gen_op_bound(LOADV, tmp))))));
} This is the reason that the RHS of Compare to Exp "|=" Exp {
$$ = gen_call("_modify", BLOCK(gen_lambda($1), gen_lambda($3)));
} | Ok, let's translate all of this to English:
The difference between |
|
I should really write this up in the wiki... |
@nicowilliams thanks a lot for the writeup! will surely try wrap my head around it once i'm back home |
Think i understand a bit more now. Ill try explain it back to you again :) each path expression outputted by LHS gets the same RHS value, and that value is evaluated before evaluating LHS? but so would it be possible to "wait" with evaluating RHS until we know there is at least one LHS output somehow? |
Yes, it should be possible. If the RHS has side-effects, that would be desirable. |
Describe the bug
RHS of a update-assignment
//=
expression is always evaluated even if LHS isnull
orfalse
. As jq has some non-pure functions likeinput
anddebug
it might cause unexpected behaviour. For example I think it should be possible to usedebug
so see if RHS gets evaluated or not.To Reproduce
Expected behavior
That RHS is only evaluated if LHS is
null
orfalse
.Environment:
macOS 12.1
Was discovered when discussing #2407
The text was updated successfully, but these errors were encountered: