Automatic insertion into lists
If you are using XSDs, for properties that represent child elements that can appear more than once, Gosu exposes that property as a list. For properties that Gosu exposes as list properties, Gosu provides a special shorthand syntax for inserting items into the list. If you assign to the list index equal to the size of the list, the index assignment becomes an insertion.
This behavior is also true if the size of the list is zero: use the [0]
array/list index notation and set the property. This syntax inserts the value into the list,
which is equivalent to dynamically adding an element to the list, meaning that you do not have
to check whether the list already exists. If you are creating XML objects in Gosu, by default
the lists do not yet exist and are initialized to null.
You use this syntax to add an element:
element.PropertyName[0] = childElementIf the list does not yet exist for a list property, Gosu creates the list when you perform the first insertion.
In the following example, List1 contains an unbounded number of instances of the
Child1 child element. Because the XSD declares that there can be more than
one instance of the child element, the List1.Child1 property is a list of
integers.
Example XSD
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Element1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Child1" type="xsd:int" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>Code
uses examples.pl.gosu.xml.myschema.List1
var xml = new List1()
print("Before insertion: ${xml.Child1.Count}")
xml.Child1[0] = 0
xml.Child1[1] = 1
xml.Child1[2] = 2
print("After insertion: ${xml.Child1.Count}")
xml.print()Output
Before insertion: 0
After insertion: 3
<?xml version="1.0"?>
<list1>
<Child1>0</Child1>
<Child1>1</Child1>
<Child1>2</Child1>
</list1>See also
