mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Improve sink and spreading docs
This commit is contained in:
parent
9a9da80665
commit
8bf0bb5da9
@ -85,6 +85,7 @@ a table listing all syntax that is available in code mode:
|
||||
| Field access | `{x.y}` | [Scripting]($scripting/#fields) |
|
||||
| Method call | `{x.flatten()}` | [Scripting]($scripting/#methods) |
|
||||
| Function call | `{min(x, y)}` | [Function]($type/function) |
|
||||
| Argument spreading | `{min(..nums)}` | [Arguments]($type/arguments) |
|
||||
| Unnamed function | `{(x, y) => x + y}` | [Function]($type/function) |
|
||||
| Let binding | `{let x = 1}` | [Scripting]($scripting/#bindings) |
|
||||
| Named function | `{let f(x) = 2 * x}` | [Function]($type/function) |
|
||||
|
@ -1066,26 +1066,36 @@ whose fields have the values of the given arguments.
|
||||
# Arguments
|
||||
Captured arguments to a function.
|
||||
|
||||
## Argument Sinks
|
||||
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 as `..sink`. The resulting `sink` value is of the `arguments` type. It
|
||||
exposes methods to access the positional and named arguments and is iterable
|
||||
with a [for loop]($scripting/#loops). Inversely, you can spread
|
||||
arguments, arrays and dictionaries into a function call with the spread operator:
|
||||
`{func(..args)}`.
|
||||
exposes methods to access the positional and named arguments.
|
||||
|
||||
## Example
|
||||
```example
|
||||
#let format(title, ..authors) = [
|
||||
*#title* \
|
||||
_Written by #(authors
|
||||
#let format(title, ..authors) = {
|
||||
let by = authors
|
||||
.pos()
|
||||
.join(", ", last: " and "));._
|
||||
]
|
||||
.join(", ", last: " and ")
|
||||
|
||||
[*#title* \ _Written by #by;_]
|
||||
}
|
||||
|
||||
#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
|
||||
### pos()
|
||||
Returns the captured positional arguments as an array.
|
||||
|
Loading…
x
Reference in New Issue
Block a user