# Destructuring assignment ## Function Destructuring assignment is used to take out multiple values from array or object at one time and assign them to variables. The left side uses tuple-like parentheses, but BT currently does not provide an independent tuple type, and the value on the right side still maintains the original array or object type. ## Syntax ```bt (a b) = value (a, b) = value ``` Space separation and comma separation are equivalent, and can also be mixed: ```bt (a, b c) = value ``` ## Parameters - Left-hand variables: A list of variable names in brackets. The first version only supports ordinary variable names. - Right side value: must be array or object. ## Return Value The return value of the destructuring assignment expression is the value on the right side, which is consistent with the ordinary assignment expression. Array destructuring reads in subscript order. When the number of variables is less than the length of the array, the extra elements will be ignored; when the number of variables is more than the length of the array, the missing position is assigned the value `empty`. Object destructuring reads the field with the same name of the object according to the variable name on the left. When the field does not exist, the corresponding variable is assigned the value `empty`. ## Code Examples Array destructuring: ```bt arr = [1 2 3] (a b c d) = arr echo a // 1 echo b // 2 echo c // 3 echo d // empty ``` Object destructuring: ```bt obj = { id: 23 name: 'Alice' age: 18 sex: 1 } (name sex missing) = obj echo name // Alice echo sex // 1 echo missing // empty ``` Single field object destructuring will maintain reference semantics: ```bt obj = { data: { name: 'Alice' } } (data) = obj data.name = 'Bob' echo obj.data.name // Bob ``` The assignment expression returns the right-hand value: ```bt arr = [1 2] result = ((a b) = arr) echo result[0] // 1 echo a // 1 ``` For loop destructuring the current value: ```bt users = [ {name: 'Alice', age: 18} {name: 'Bob', age: 20} ] for (name age) in users { echo name echo age } ``` Loop destructuring only acts on the value obtained in each iteration and does not change the semantics of ordinary `for key,value in users`. ## Notes is not array or object, a runtime error will occur: ```bt (a b) = 123 ``` Error message: ```text 解构赋值右侧必须是 array 或 object ```