Collections
Collections make it possible to store data of different types together. Besides, they can be used to initiate arrays and any other structures. Also, collections can be used to pass an undefined number of parameters of different types to functions and methods. The collection type corresponds to collections. Collections are defined with braces %{ ... }. You can specify different types of data or other collections separating them by commas insides braces. Global variables can be initialized by collections which contain only constants.
global
{
arrstr months = %{"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" }
}
In order to initialize a structure with the help of collection, the appropriate assignment operator is required to be defined.
type test
{
uint num
str string
}
operator test =( test left, collection right )
{
if right.gettype( 0 ) != uint : return left
left.num = right.val( 0 )
if right.gettype( 1 ) != str : return left
left.string = right.val( 1 )->str
return left
}
After that, a value is assigned to the fields, as follows:
test myt
myt = %{ 10, "test string" }
Using the collection argument in the function, you are able to pass a variable number of arguments of different types.
func outvals( collection cl )
{
uint i
fornum i,*cl
{
print("\(i) = \(cl[ i ])\n")
}
}
The function call has the form.
outvals( %{ 10, 20, 30, 40 })