diff --git a/docs/content/manual/dev/manual.yml b/docs/content/manual/dev/manual.yml index e7150c41..27ab1fc8 100644 --- a/docs/content/manual/dev/manual.yml +++ b/docs/content/manual/dev/manual.yml @@ -3072,6 +3072,16 @@ sections: to be evaluated once per value in the value of `.` at the call site. + Multiple arguments are separated by semicolons, both in the + definition and in the call: + + def f(a; b): ...; + f(.x; .y) + + A comma does not separate arguments. `f(.x, .y)` calls a + one-argument `f` with the single filter `.x, .y`, which + outputs `.x` and then `.y` (see the comma operator above). + Multiple definitions using the same function name are allowed. Each re-definition replaces the previous one for the same number of function arguments, but only for references from @@ -3085,6 +3095,12 @@ sections: - program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])' input: '[[1,2],[10,20]]' output: ['[[1,2,1,2], [10,20,1,2]]'] + - program: 'def f(a; b): [a, b]; f(.x; .y)' + input: '{"x":1,"y":2}' + output: ['[1,2]'] + - program: 'def f(a): [a]; f(.x, .y)' + input: '{"x":1,"y":2}' + output: ['[1,2]'] - title: 'Scoping' body: | diff --git a/jq.1.prebuilt b/jq.1.prebuilt index be0b8fab..2c47856b 100644 --- a/jq.1.prebuilt +++ b/jq.1.prebuilt @@ -3522,6 +3522,23 @@ def addvalue($f): \.\.\.; With either definition, \fBaddvalue(\.foo)\fR will add the current input\'s \fB\.foo\fR field to each element of the array\. Do note that calling \fBaddvalue(\.[])\fR will cause the \fBmap(\. + $f)\fR part to be evaluated once per value in the value of \fB\.\fR at the call site\. . .P +Multiple arguments are separated by semicolons, both in the definition and in the call: +. +.IP "" 4 +. +.nf + +def f(a; b): \.\.\.; +f(\.x; \.y) +. +.fi +. +.IP "" 0 +. +.P +A comma does not separate arguments\. \fBf(\.x, \.y)\fR calls a one\-argument \fBf\fR with the single filter \fB\.x, \.y\fR, which outputs \fB\.x\fR and then \fB\.y\fR (see the comma operator above)\. +. +.P Multiple definitions using the same function name are allowed\. Each re\-definition replaces the previous one for the same number of function arguments, but only for references from functions (or main program) subsequent to the re\-definition\. See also the section below on scoping\. . .IP "" 4 @@ -3535,6 +3552,14 @@ jq \'def addvalue(f): \. + [f]; map(addvalue(\.[0]))\' jq \'def addvalue(f): f as $x | map(\. + $x); addvalue(\.[0])\' [[1,2],[10,20]] => [[1,2,1,2], [10,20,1,2]] + +jq \'def f(a; b): [a, b]; f(\.x; \.y)\' + {"x":1,"y":2} +=> [1,2] + +jq \'def f(a): [a]; f(\.x, \.y)\' + {"x":1,"y":2} +=> [1,2] . .fi . diff --git a/tests/man.test b/tests/man.test index 86243105..e2b3b064 100644 --- a/tests/man.test +++ b/tests/man.test @@ -888,6 +888,14 @@ def addvalue(f): f as $x | map(. + $x); addvalue(.[0]) [[1,2],[10,20]] [[1,2,1,2], [10,20,1,2]] +def f(a; b): [a, b]; f(.x; .y) +{"x":1,"y":2} +[1,2] + +def f(a): [a]; f(.x, .y) +{"x":1,"y":2} +[1,2] + isempty(empty) null true