Improve sink and spreading docs

This commit is contained in:
Laurenz 2023-07-05 11:42:23 +02:00
parent 9a9da80665
commit 8bf0bb5da9
2 changed files with 21 additions and 10 deletions

View File

@ -85,6 +85,7 @@ a table listing all syntax that is available in code mode:
| Field access | `{x.y}` | [Scripting]($scripting/#fields) | | Field access | `{x.y}` | [Scripting]($scripting/#fields) |
| Method call | `{x.flatten()}` | [Scripting]($scripting/#methods) | | Method call | `{x.flatten()}` | [Scripting]($scripting/#methods) |
| Function call | `{min(x, y)}` | [Function]($type/function) | | Function call | `{min(x, y)}` | [Function]($type/function) |
| Argument spreading | `{min(..nums)}` | [Arguments]($type/arguments) |
| Unnamed function | `{(x, y) => x + y}` | [Function]($type/function) | | Unnamed function | `{(x, y) => x + y}` | [Function]($type/function) |
| Let binding | `{let x = 1}` | [Scripting]($scripting/#bindings) | | Let binding | `{let x = 1}` | [Scripting]($scripting/#bindings) |
| Named function | `{let f(x) = 2 * x}` | [Function]($type/function) | | Named function | `{let f(x) = 2 * x}` | [Function]($type/function) |

View File

@ -1066,26 +1066,36 @@ whose fields have the values of the given arguments.
# Arguments # Arguments
Captured arguments to a function. Captured arguments to a function.
## Argument Sinks
Like built-in functions, custom functions can also take a variable number of Like built-in functions, custom functions can also take a variable number of
arguments. You can specify an _argument sink_ which collects all excess arguments. You can specify an _argument sink_ which collects all excess
arguments as `..sink`. The resulting `sink` value is of the `arguments` type. It arguments as `..sink`. The resulting `sink` value is of the `arguments` type. It
exposes methods to access the positional and named arguments and is iterable exposes methods to access the positional and named arguments.
with a [for loop]($scripting/#loops). Inversely, you can spread
arguments, arrays and dictionaries into a function call with the spread operator:
`{func(..args)}`.
## Example
```example ```example
#let format(title, ..authors) = [ #let format(title, ..authors) = {
*#title* \ let by = authors
_Written by #(authors
.pos() .pos()
.join(", ", last: " and "));._ .join(", ", last: " and ")
]
[*#title* \ _Written by #by;_]
}
#format("ArtosFlow", "Jane", "Joe") #format("ArtosFlow", "Jane", "Joe")
``` ```
## Spreading
Inversely to an argument sink, you can _spread_ arguments, arrays and
dictionaries into a function call with the `..spread` operator:
```example
#let array = (2, 3, 5)
#calc.min(..array)
#let dict = (fill: blue)
#text(..dict)[Hello]
```
## Methods ## Methods
### pos() ### pos()
Returns the captured positional arguments as an array. Returns the captured positional arguments as an array.