Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructuring arrays and objects. #684

Open
StarArawn opened this issue Dec 25, 2022 · 1 comment
Open

Destructuring arrays and objects. #684

StarArawn opened this issue Dec 25, 2022 · 1 comment

Comments

@StarArawn
Copy link

StarArawn commented Dec 25, 2022

It would be nice if we had syntax to destructure arrays, objects and other types.

Syntax could look something like this:

let my_array = [0, 1, 2];
let [a, b, c] = my_array;

print(a); // 0
print(b); // 1
print(c); // 2

This is similar to JavaScript and this also lets you emulate tuples! Like:

let my_tuple = ["Hello", "World!"];
let [hello, world] = my_tuple;
print(hello); // Hello
print(world); // World!
@schungx
Copy link
Collaborator

schungx commented Dec 25, 2022

This might be doable as Rhai only contains two literal syntax that can be matched against: arrays and maps. I'll look into that.

But then, once we match on arrays, we probably need the spread operator ... to do whacky things like match both ends, match the middle etc.

So it must consider all possible combinations: [a, b, c], [a, b, ...], [..., b, c], [a, ... c], [..., b, ...]...

And if you add _ to match one slot, then there will be many more patterns to handle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants