From 9c2599cb11346602f84d32a2e327580547f19fa1 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sun, 30 Jan 2022 18:02:45 -0500 Subject: Document destructuring assignment --- docs/doc/expression.html | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'docs/doc/expression.html') diff --git a/docs/doc/expression.html b/docs/doc/expression.html index 6b7f588c..c1b4d539 100644 --- a/docs/doc/expression.html +++ b/docs/doc/expression.html @@ -173,7 +173,7 @@

A function with an asterisk indicates that a subject can also be used. Since the role doesn't exist after parsing, function and subject spellings are indistinguishable in these positions. Modifier applications bind more tightly than functions, and associate left-to-right while functions associate right-to-left.

-

Assignment

+

Assignment

Another element that can be included in expressions is assignment, which is written with to define (also called "declare" in many other languages) a variable and to change its definition. A variable can only be defined once within a scope, and can only be changed if it has already been defined. However, it can be shadowed, meaning that it is defined again in an inner scope even though it has a definition in an outer scope already.

↗️
    x1  {x2  x3  x}
 3
@@ -205,6 +205,25 @@
 
 
 
+

Destructuring

+

The left hand side of assignment in a subject expression can be compound, so that assigned values are extracted from lists or namespaces. This is called a destructuring assignment. The most common case is list destructuring: the left hand side's written as a list, and the value on the right has to be a list of the same length. Assignments are made element-wise, and the elements can destructure things further.

+↗️
    qr,s  "qr",4
+⟨ "qr" ⟨ 0 1 2 3 ⟩ ⟩
+
+    r
+'r'
+
+    s
+⟨ 0 1 2 3 ⟩
+
+

Namespace destructuring uses an overlapping syntax, fully described in its own section. The left hand side is a list of names or aliases tofrom.

+↗️
    qr  {q2+r0.5}  q
+2.5
+
+

With destructuring, you might want to discard some values from the right hand side rather than assign them any name. There's special syntax for this: use Nothing (·) for a placeholder non-name in the appropriate position, like ·y· list.

+↗️
    ·  6   # Doesn't do anything
+6
+

Exports

The double arrow is used to export variables from a block or program, causing the result to be a namespace. There are two ways to export variables. First, in the variable definition can be replaced with to export the variable as it's defined. Second, an export statement consisting of an assignment target followed by with nothing to the right exports the variables in the assignment target and does nothing else. Export statements can be placed anywhere in the relevant program or body, including before declaration or on the last line, and a given variable can be exported any number of times.

aliasa, b, c0c1c, b2b{
-- 
cgit v1.2.3