summaryrefslogtreecommitdiff
path: root/docs/src/reference/scripting.md
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-04-26 13:46:42 +0200
committerLaurenz <laurmaedje@gmail.com>2023-04-26 15:37:21 +0200
commit3680c854a21db665d64cdb8f31aa0f9a1af16ceb (patch)
tree39dfa33059293251f1e2890f9b3d0e3dc178ed03 /docs/src/reference/scripting.md
parent59957746e91c1322a8ca6d228bcaa0f31941ee1b (diff)
Touch up docs
Diffstat (limited to 'docs/src/reference/scripting.md')
-rw-r--r--docs/src/reference/scripting.md38
1 files changed, 35 insertions, 3 deletions
diff --git a/docs/src/reference/scripting.md b/docs/src/reference/scripting.md
index d125bf5b..6af17cc7 100644
--- a/docs/src/reference/scripting.md
+++ b/docs/src/reference/scripting.md
@@ -64,7 +64,7 @@ Content and code blocks can be nested arbitrarily. In the example below,
}
```
-## Let bindings { #bindings }
+## Bindings and Destructuring { #bindings }
As already demonstrated above, variables can be defined with `{let}` bindings.
The variable is assigned the value of the expression that follows the `=` sign.
The assignment of a value is optional, if no value is assigned, the variable
@@ -82,7 +82,10 @@ Sum is #add(2, 3).
```
Let bindings can also be used to destructure [arrays]($type/array) and
-[dictionaries]($type/dictionary).
+[dictionaries]($type/dictionary). In this case, the left-hand side of the
+assignment should mirror an array or dictionary. The `..` operator can be used
+once in the pattern to collect the remainder of the array's or dictionary's
+items.
```example
#let (x, y) = (1, 2)
@@ -117,6 +120,28 @@ You can use the underscore to discard elements in a destructuring pattern:
The y coordinate is #y.
```
+Destructuring also work in argument lists of functions ...
+
+```example
+#let left = (2, 4, 5)
+#let right = (3, 2, 6)
+#left.zip(right).map(
+ ((a,b)) => a + b
+)
+```
+
+... and on the left-hand side of normal assignments. This can be useful to
+swap variables among other things.
+
+```example
+#{
+ let a = 1
+ let b = 2
+ (a, b) = (b, a)
+ [a = #a, b = #b]
+}
+```
+
## Conditionals { #conditionals }
With a conditional, you can display or compute different things depending on
whether some condition is fulfilled. Typst supports `{if}`, `{else if}` and
@@ -206,12 +231,19 @@ can be either:
- a [dictionary]($type/dictionary) that has the specified key,
- a [symbol]($type/symbol) that has the specified modifier,
- a [module]($type/module) containing the specified definition,
-- [content]($type/content) that has the specified field.
+- [content]($type/content) consisting of an element that has the specified
+ field. The available fields match the arguments of the
+ [element function]($type/function/#element-functions) that were given when
+ the element was constructed.
```example
#let dict = (greet: "Hello")
#dict.greet \
#emoji.face
+
+#let it = [= Heading]
+#it.body \
+#it.level
```
## Methods { #methods }