8000 Proposals: Destructuring assignment · Issue #560 · TriggerReactor/TriggerReactor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Proposals: Destructuring assignment #560

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

Open
Sayakie opened this issue Jul 14, 2023 · 0 comments
Open

Proposals: Destructuring assignment #560

Sayakie opened this issue Jul 14, 2023 · 0 comments
Assignees
Labels
backlog Project which can't be done in short amount of time or which need more discussion. category:feature-request A feature request, i.e: not implemented / a PR. domain:suggestion A suggestion, i.e: new ideas, optimization, etc. package:core Related to share domain nor script interpreter priority:low

Comments

@Sayakie
Copy link
Member
Sayakie commented Jul 14, 2023

Unpacks values from arrays into distinct variables. This capability is similar to features present in languages such as JavaScript, Perl and Python.

Syntax

fruits = arrayOf("apple", "banana", "blueberry")

[apple, banana, blueberry] = fruits
#LOG apple      // Prints "apple"
#LOG banana     // Prints "banana"
#LOG blueberry  // Prints "blueberry"
balls = arrayOf("PokeBall", "DiveBall", "GreatBall", "MasterBall")

[pokeball, , , masterball] = balls
#LOG pokeball    // Prints "PokeBall"
#LOG masterball  // Prints "MasterBall"

[pokeball, ...others] = balls
#LOG others             // Prints "[DiveBall, GreatBall, MasterBall]"
#LOG others.length      // Prints "3"
#LOG others IS Array    // Prints "true"
#LOG others.getClass()  // Prints "java.lang.String[]"

Default value

Each destructured property can have a default value. The default value is used when the property is not present, or has value null.

[a, b = 2] = arrayOf(1)

#LOG b  // Prints "2"

The default value MUST be type of the right-hand side. So the following code will be thrown an exception:

[a, b = "2"] = arrayOf(1)

// Raise ParserException caused by ArrayStoreException

Rest property

As you can see in syntax section, you can end a destructuring pattern with a rest property(...rest). This pattern will store all remaining properties of the array into a new array.

[pokeball, ...others] = balls
#LOG others             // Prints "[DiveBall, GreatBall, MasterBall]"
#LOG others.length      // Prints "3"
#LOG others IS Array    // Prints "true"
#LOG others.getClass()  // Prints "java.lang.String[]"

The rest property must be last in the pattern, and must not have atrailing command.

[pokeball, ...others, ] = balls

// Raise ParserException
// Message probably like: Rest element may not have a trailing comma.
@Sayakie Sayakie added domain:suggestion A suggestion, i.e: new ideas, optimization, etc. backlog Project which can't be done in short amount of time or which need more discussion. priority:low package:core Related to share domain nor script interpreter category:feature-request A feature request, i.e: not implemented / a PR. labels Jul 14, 2023
@Sayakie Sayakie self-assigned this Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog Project which can't be done in short amount of time or which need more discussion. category:feature-request A feature request, i.e: not implemented / a PR. domain:suggestion A suggestion, i.e: new ideas, optimization, etc. package:core Related to share domain nor script interpreter priority:low
Projects
None yet
Development

No branches or pull requests

1 participant
0