next up previous contents index
Next: The grid constraint Up: Arrays Previous: Writing loops

Anonymous arrays

An anonymous array is a list of elements in the form of {} where each element can be of any type. Anonymous arrays can be used only as arguments in calls to user-defined constraints. To the definition side of constraints, anonymous arrays are just the same as usual arrays. The following example illustrates how to use anonymous arrays.

       class RightAlignedHelloWorlds {
             Label lb {text == "Hello World!"};
             Button bt {text == "Hello World!"};
             TextField tf {text == "Hello World!"};
             
             rightAligned({lb,bt,tf});
       }
       constraint rightAligned(Object[] objs){
            for (i in 0 .. objs.length-2){
                  objs[i].x+objs[i].width==objs[i+1].x+objs[i+1].width;
            }
        }
   

Because elements of anonymous arrays can be heterogeneous, we sometimes want to write different constraints depending on the types of elements. To check element types, we use the X instanceof Y constraint, which is true when X is an instance of class Y.  



Neng-Fa ZHOU
Sat Apr 18 16:14:29 JST 1998