Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10788

Consider adding Object deconstruction a-la Javascript

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • None
    • None

    Description

      In JS you can perform following operations on Arrays:

      const [ a, b, c ] = [ 1, 2, 3 ]
      

      which corresponds to mutli-assignment in Groovy.

      However JS does some really nice syntactic sugar with Object deconstruction on top:

      // "forward"
      const object = { question:'about life', answer:42, z:11 }
      const { question, answer } = object
      
      // "backward"
      const newObject = { question, answer }
      

      This way you can save a lot of code-duplication between declaring variables and Map keys.

      The syntax could look like:

      // "forward"
      def object = [ question:'about life', answer:42, z:11 ]
      def ( question, answer ) = object
      
      // "backward"
      def newObject = [ :question, answer: ]

      What do you think?

      Attachments

        Activity

          People

            Unassigned Unassigned
            injecteer@gmail.com Konstantin Smirnov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: