amber-lang / amber

💎 Amber the programming language compiled to bash
https://amber-lang.com
GNU General Public License v3.0
3.66k stars 77 forks source link

[Feature] Array Destructuring #239

Open UrbanCoffee opened 3 weeks ago

UrbanCoffee commented 3 weeks ago

Array destructuring syntax provides a quick way to assign elements of an array to multiple variables in a single line.

let array = [1,2,3,4];
let [ a, b, ...c ] = array;
echo a // "1"
echo b // "2"
echo c // "3 4"

let d = 0
let e = 0
[ d, e ] = c
echo d // 3
echo e // 4

Amber could support:

This Gist by JTBrinkmann demonstrates how a destructuring function could be implemented. However, each implementation involves globally declaring variables which would be troublesome when used inside a function.

b1ek commented 3 weeks ago

this kind of split should be done compile time like this without the need to create a function and assign variables globally

let [ a, b ] = array;
let a = array[0];
let b = array[1];