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

It should be possible to type-pin locals in QML functions

    XMLWordPrintable

Details

    Description

      Currently, it's rather cumbersome to tell the QML compiler that a local value in a function will always have the same type. You need to as-cast whenever you assign null or something that cannot be statically proven to be of the required type. Type assertions on locals would come in handy here.

      Case in point:

          property Variable prev
          property Variable first
          property Variable last
          function chainTest(n: int) {
            planner = newPlanner.createObject(null) as Planner;
            prev = null;
            first = null;
            last = null;
      
            // Build chain of n equality constraints
            for (var i = 0; i <= n; i++) {
              var name = "v" + i;
              var v = newVariable.createObject(null, {name: name}) as Variable;
              if (prev != null)
                newEqualityConstraint.createObject(null, {v1: prev, v2: v, strength: Strength.REQUIRED});
              if (i == 0) first = v;
              if (i == n) last = v;
              prev = v;
            }
      
            newStayConstraint.createObject(null, {myOutput: last, strength: Strength.STRONG_DEFAULT});
            var edit = newEditConstraint.createObject(null, {myOutput: first, strength: Strength.PREFERRED}) as EditConstraint;
            var edits = newOrderedCollection.createObject(null) as OrderedCollection;
            edits.add(edit);
            var plan = planner.extractPlanFromConstraints(edits);
            for (var i = 0; i < 100; i++) {
              first.value = i;
              plan.execute();
              if (last.value != i) // TODO: Bogus compile error!
                console.error("Chain test failed.");
            }
          }
      

      Here, I've factored out the locals into the surrounding object, and it still doesn't help as much as I would hope.

      Attachments

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

        Activity

          People

            qtqmlteam Qt Qml Team User
            ulherman Ulf Hermann
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes