Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Object destructuring in QML?
QtWS25 Last Chance

Object destructuring in QML?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 2 Posters 946 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtPai
    wrote on 30 Jul 2020, 13:55 last edited by
    #1

    According to the docs the QML engine implements the Standard ECMA-262. This standard includes Array and Object destructuring with the "rest" operator, like so:

    const obj1 = { a: 1, b: 2 }
    const obj2 = { ...obj1, c: 3, d: 4 }  // which returns { a: 1, b: 2, c: 3, d: 4 }
    

    However when I try to do this I get an error "Invalid token ..." in QtCreator. I does work with Arrays, though.

    I neither see any mention in the docs stating that restructuring objects is not implemented in QML, nor I found any mention about it.

    Anyone has some pointers about this issue?

    I'm using the latest QtCreator and QML versions.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcarney
      wrote on 30 Jul 2020, 14:14 last edited by
      #2

      "7th edition of the standard"

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QtPai
        wrote on 30 Jul 2020, 15:21 last edited by
        #3

        Hi and thanks for the quick reply.

        The thing is that the rest/spread operators are part of ES6, the sixth version of the standard. So ES7 already has them by default.

        I can use lambdas, "const", "let" and I can even use rest/spread with Array: [ ...arrayObj ]. They all work in my QML. But not the Object rest/spread operator: { ...plainObj }

        I mean, of course the docs may be wrong then, and perhaps that is the thing. But yet it doesn't make sense because there is not ECMA version where Array destructuring works but Object destructuring doesn't. Perhaps something is missing?

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 30 Jul 2020, 15:25 last edited by
          #4

          What version of Qt are you testing this? I wonder if the docs only match 5.15's version of ecma.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fcarney
            wrote on 30 Jul 2020, 15:30 last edited by fcarney
            #5

            Mozilla says object spread was introduced in 2018:
            https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
            I don't think the the ecma script in qt is that new.

            Edit:
            https://en.wikipedia.org/wiki/ECMAScript
            7th edition 2016

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QtPai
              wrote on 30 Jul 2020, 18:50 last edited by
              #6

              I think Mozilla points to the ECMA entry page.

              The official spec definitely has it: http://www.ecma-international.org/ecma-262/6.0/ (and are nicely explained here)

              I know this to be the case cause I've been using rest/spread since early 2017 when we migrated our NodeJS codebase at work, first with Babel transpiling and later natively when Node implemented the full ES6.

              And like I said, in QML I'm able to use rest/spread syntax but only with Arrays, not plain Objects.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fcarney
                wrote on 30 Jul 2020, 18:56 last edited by
                #7

                From the wiki page I linked:

                9th Edition – ECMAScript 2018
                
                The 9th edition, officially known as ECMAScript 2018, was finalized in June 2018.[16] New features include rest/spread operators for object literals (three dots: ...identifier), asynchronous iteration, Promise.prototype.finally and additions to RegExp.[16]
                
                The spread operator allows for the easy copying of object properties, as shown below.
                
                let object = {a: 1, b: 2}
                
                let objectClone = Object.assign({}, object) // before ES9
                let objectClone = {...object} // ES9 syntax
                
                let otherObject = {c: 3, ...object}
                console.log(otherObject) // -> {c: 3, a: 1, b: 2}
                
                

                From the page you posted:
                https://github.com/lukehoban/es6features#default--rest--spread

                Default + Rest + Spread

                Callee-evaluated default parameter values. Turn an array into consecutive arguments in a function call. Bind trailing parameters to an array. Rest replaces the need for arguments and addresses common cases more directly.

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  QtPai
                  wrote on 30 Jul 2020, 20:00 last edited by
                  #8

                  Forgot to mention, indeed I'm using Qt version 5.15. Pretty sure. Actually I was quite surprised when I read QML implemented ECMA version 7, cause it is quite recent in practice as many companies take some time to adopt these standards anyway.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fcarney
                    wrote on 30 Jul 2020, 20:01 last edited by
                    #9

                    I believe it is V8 under the hood. Just not the most recent version.

                    C++ is a perfectly valid school of magic.

                    1 Reply Last reply
                    0

                    7/9

                    30 Jul 2020, 18:56

                    • Login

                    • Login or register to search.
                    7 out of 9
                    • First post
                      7/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved