Skip to content
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

Semantics of +=, -=, ... #44

Closed
01mf02 opened this issue Oct 7, 2022 · 3 comments
Closed

Semantics of +=, -=, ... #44

01mf02 opened this issue Oct 7, 2022 · 3 comments

Comments

@01mf02
Copy link
Owner

01mf02 commented Oct 7, 2022

The jq manual states:

jq has a few operators of the form a op= b, which are all equivalent to a |= . op b.

However, I believe that this is actually not true in jq. Proof:

$ jq -cn '{x: 1, y: 2} | .x += .y'
{"x":3,"y":2}
$ jq -cn '{x: 1, y: 2} | .x |= . + .y'
jq: error (at <unknown>): Cannot index number with string "y"

What is the intended semantics of +=, -= then? Should the jq manual be updated to reflect this behaviour?
@itchyny, @pkoppstein, do you have any idea about this?

(I discovered this issue while porting @itchyny's Brainfuck interpreter to jaq, which involved changing
.output += [.memory[.pointer]] to .memory[.pointer] as $m | .output += [$m] due to this issue.)

@01mf02 01mf02 changed the title Semantics of Semantics of +=, -=, ... Oct 7, 2022
@wader
Copy link
Contributor

wader commented Oct 7, 2022

Hi, jqlang/jq#2407 might be interesting

@01mf02
Copy link
Owner Author

01mf02 commented Oct 7, 2022

Hi @wader, thanks, that is definitely interesting!
I particularly like the definition of @pkoppstein:

  • V | (E = F) is identical to V | (E = (V|F))
  • V | (E |= F) is identical to V | (E = (V|E|F))
  • V | (E += F) is identical to V | (E = (V|E) + (V|F))

This seems to be a relatively concise description of the current jq behaviour.

@01mf02
Copy link
Owner Author

01mf02 commented Oct 8, 2022

I adopted the semantics of =, +=, -=, ... that are implemented in jq as of 5908676.

@01mf02 01mf02 closed this as completed Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants