Document function limitation of var properties
authorChris Adams <christopher.adams@nokia.com>
Thu, 1 Dec 2011 06:07:59 +0000 (16:07 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 6 Dec 2011 01:46:28 +0000 (02:46 +0100)
Functions cannot be assigned to var properties due to the fact that
such an assignment signifies a binding assignment.  This limitation
needs to be documented.  One workaround is to assign an array which
contains a function element to a var property, and this commit also
adds a unit test to ensure this works.

Change-Id: I02363b88233282106ac6d26f14df1988155057b9
Reviewed-by: Martin Jones <martin.jones@nokia.com>
doc/src/declarative/basictypes.qdoc
tests/auto/declarative/qdeclarativeecmascript/data/propertyVar.6.qml

index c128239..98eea78 100644 (file)
     \brief A var type is a generic property type.
 
     A var is a generic property type capable of storing any data type.
-    It is equivalent to a regular JavaScript variable.
+    It is equivalent to a regular JavaScript variable, except that you
+    cannot assign a JavaScript function to such a property.
     For example, var properties can store numbers, strings, objects and
     arrays:
 
         property var aPoint: Qt.point(10, 10)
         property var aSize: Qt.size(10, 10)
         property var aVector3d: Qt.vector3d(100, 100, 100)
-        property var anArray: [1, 2, 3, "four", "five"]
+        property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
         property var anObject: { "foo": 10, "bar": 20 }
     }
     \endqml
 
+    Attempting to assign a JavaScript function to a var property will result in
+    a binding assignment as per other property types.  You can assign a JavaScript
+    array containing a single function element instead.
+
     It is important to note that changes in regular properties of JavaScript
     objects assigned to a var property will \bold{not} trigger updates of bindings
     that access them.  The example below will display "The car has 4 wheels" as
index 5197aea..060d24e 100644 (file)
@@ -5,13 +5,22 @@ Item {
 
     property var items: [1, 2, 3, "four", "five"]
     property int bound: items[0]
+    property var funcs: [(function() { return 6; })]
+    property int bound2: funcs[0]()
+
+    function returnTwenty() {
+        return 20;
+    }
 
     Component.onCompleted: {
         if (bound != 1) return false;
+        if (bound2 != 6) return false;
 
         items = [10, 2, 3, "four", "five"]  // bound should now be 10
+        funcs = [returnTwenty]              // bound2 should now be 20
 
         if (bound != 10) return false;
+        if (bound2 != 20) return false;
 
         test = true;
     }