Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-139463

More aggressive warnings for type mismatches in QML structured values

XMLWordPrintable

      Code

      class MyStructuredType {
          Q_GADGET
          QML_VALUE_TYPE(myStructuredType)
          QML_STRUCTURED_VALUE
      
          // NOTE: The constructor and setters print debug messages about their parameters
          Q_PROPERTY(int number READ number WRITE setNumber FINAL)
          Q_PROPERTY(bool truth READ truth WRITE setTruth FINAL)
      
      public:
          Q_INVOKABLE MyStructuredType(int number = 0, bool truth = false);
          Q_INVOKABLE QString toString() const;
          
          ...
      }
      
      import QtQuick
      import ValueTypeTest
      
      Window {
          width: 640
          height: 480
          visible: true
      
          property myStructuredType t: ({
              number: "42",
              truth: "false",
              nonexistentProp: "Oops"
          })
      
          Component.onCompleted: {
              console.log("=== Component Complete ===")
              console.log(t)
          }
      }
      

       

      Outcomes

      Tooling:

      • qmlsc does warn that "MyStructuredType has no property called nonexistentProp [compiler]". However, this is easily missed since qmlsc is currently silent by default.
      • qmllint and qmlls don't warn about nonexistentProp

       

      Runtime:

      ...
      Setting number: 42
      Setting truth: true
      qml: === Component Complete ===
      ...
      qml: myStructuredType(42, true)
      

      The implicit conversion of "42" to 42 and "false" to true (for passing to the property setter functions) match the documented type coercion rules for JavaScript. However, this can be quite unintuitive.

       

      Suggestions

      • Let qmllint and qmlls warn about non-existent properties too
      • Let the tools offer warnings about implicit type coercions (where type mismatches occur), even if the type coercion rules are well-defined.
        • Given that "flexibility" is an inherent quality of JavaScript, perhaps this should be opt-in and only limited to QML constructible/structured values

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            qtqmlteam Qt Qml Team User
            skoh-qt Sze Howe Koh
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes