You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Konstantin Smirnov (Jira)" <ji...@apache.org> on 2022/10/07 12:07:00 UTC
[jira] [Comment Edited] (GROOVY-10788) Consider adding Object deconstruction a-la Javascript
[ https://issues.apache.org/jira/browse/GROOVY-10788?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17614050#comment-17614050 ]
Konstantin Smirnov edited comment on GROOVY-10788 at 10/7/22 12:06 PM:
-----------------------------------------------------------------------
[~emilles]
{code:groovy}
def m=[a:1, b:2]
def m1=[c:3, d:4]
def m_m1 = [ *:m, *:m1 ]
assert m_m1.toString() == '[a:1, b:2, c:3, d:4]'{code}
this is essentially what I wanted to have as a replacement for JS's
{code:javascript}
const copy = [ ...object, ...obj2 ]
{code}
Perfect!
What about multiple assignment of Map values to variables, based on var name to key match:
{code:groovy}
def ( question, answer ) = map{code}
and vice-versa, when the var name is used as a key in a Map:
{code:groovy}
def a = 1
def b = 2
def m = [ :a, b: ]
assert m.toString() == '[a:1, b:2]' {code}
?
Is that possible?
was (Author: JIRAUSER296633):
[~emilles]
{code:groovy}
def m=[a:1, b:2]
def m1=[c:3, d:4]
def m_m1 = [ *:m, *:m1 ]
assert m_m1.toString() == '[a:1, b:2, c:3, d:4]'{code}
this is essentially what I wanted to have as a replacement for JS's
{code:javascript}
const copy = [ ...object, ...obj2 ]
{code}
Perfect!
What about multiple assignment of Map values to variables, based on var name to key match:
{code:java}
def ( question, answer ) = map{code}
and vice-versa, when the var name is used as a key in a Map:
{code:groovy}
def a = 1
def b = 2
def m = [ :a, b: ]
assert m.toString() == '[a:1, b:2]' {code}
?
Is that possible?
> Consider adding Object deconstruction a-la Javascript
> -----------------------------------------------------
>
> Key: GROOVY-10788
> URL: https://issues.apache.org/jira/browse/GROOVY-10788
> Project: Groovy
> Issue Type: Improvement
> Reporter: Konstantin Smirnov
> Priority: Major
>
> In JS you can perform following operations on Arrays:
> {code:java}
> const [ a, b, c ] = [ 1, 2, 3 ]
> {code}
> which corresponds to mutli-assignment in Groovy.
> However JS does some really nice syntactic sugar with Object deconstruction on top:
> {code:javascript}
> // "forward"
> const object = { question:'about life', answer:42, z:11 }
> const { question, answer } = object
> // "backward"
> const newObject = { question, answer }
> {code}
> This way you can save a lot of code-duplication between declaring variables and Map keys.
> The syntax could look like:
> {code:groovy}
> // "forward"
> def object = [ question:'about life', answer:42, z:11 ]
> def ( question, answer ) = object
> // "backward"
> def newObject = [ :question, answer: ]{code}
> What do you think?
--
This message was sent by Atlassian Jira
(v8.20.10#820010)