docs: explain that semicolons separate function arguments (#3685)
The manual's "Defining Functions" section shows only one-argument functions, so it never says how to pass more than one. This adds a short paragraph: arguments are separated by `;` in both `def f(a; b):` and `f(.x; .y)`, and `f(.x, .y)` instead calls a one-argument `f` with the filter `.x, .y`. It also adds an example for each. `tests/man.test` and `jq.1.prebuilt` are regenerated. Fixes #2163
This commit is contained in:
1 parent
e440cccc0a
commit
49fbca1242
3 files changed
+49
No files matched your search
@@ -3072,6 +3072,16 @@ sections:
|
|||||||
to be evaluated once per value in the value of `.` at the call
|
to be evaluated once per value in the value of `.` at the call
|
||||||
site.
|
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.
|
Multiple definitions using the same function name are allowed.
|
||||||
Each re-definition replaces the previous one for the same
|
Each re-definition replaces the previous one for the same
|
||||||
number of function arguments, but only for references from
|
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])'
|
- program: 'def addvalue(f): f as $x | map(. + $x); addvalue(.[0])'
|
||||||
input: '[[1,2],[10,20]]'
|
input: '[[1,2],[10,20]]'
|
||||||
output: ['[[1,2,1,2], [10,20,1,2]]']
|
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'
|
- title: 'Scoping'
|
||||||
body: |
|
body: |
|
||||||
|
|||||||
Generated
+25
@@ -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\.
|
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
|
.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\.
|
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
|
.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])\'
|
jq \'def addvalue(f): f as $x | map(\. + $x); addvalue(\.[0])\'
|
||||||
[[1,2],[10,20]]
|
[[1,2],[10,20]]
|
||||||
=> [[1,2,1,2], [10,20,1,2]]
|
=> [[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
|
.fi
|
||||||
.
|
.
|
||||||
|
|||||||
Generated
+8
@@ -888,6 +888,14 @@ def addvalue(f): f as $x | map(. + $x); addvalue(.[0])
|
|||||||
[[1,2],[10,20]]
|
[[1,2],[10,20]]
|
||||||
[[1,2,1,2], [10,20,1,2]]
|
[[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)
|
isempty(empty)
|
||||||
null
|
null
|
||||||
true
|
true
|
||||||
|
|||||||
Reference in new issue
Block a user