4D initiates a new variable type: Collection

4D v16 R4 introduces a new type of variable called Collection. What are these variables intended for? Like Objects or BLOBs, collections can be used to hold almost any kind of variable types like texts, numerics, booleans, objects and even other collections. The good thing is that, all these variables types previously mentioned can be mixed inside the same collection, where arrays must always contain the same type of elements!

What’s new?

Two new commands have been created (C_COLLECTION and New collection). The content of collections can be accessed and modified using the object notation. The collections can be compared to arrays but they are way more versatile as they are not limited to only one type of data.

The collections can be stringified and strings can be parsed to create collections. To do so, just use the JSON Stringify and JSON Parse existing commands. If you are used to manipulate JSON Arrays you will find out that collections are very similar. If you are not, you will get used to them in a very short time.

Two main differences to be noticed:

1. In order to access items of a collection you must use square brackets [ ] , and NOT curly brackets { }.

2. The collection elements start at indice 0. So the first item can be accessed by writing collection[0] which is different from arrays which first element is 1.

On one hand, arrays (whose elements are all the same type) allow memory optimization, so they are fast and efficient. On the other hand, collections are providing high flexibility. So, if memory or speed maters, use arrays. If you want to code easier, use collections. You now have a choice.

Beside that, in most cases, collections can be used the same way as other type of variables in 4D! They can be sent as parameters to methods, received as function returns, declared as local or global, etc.

Example Database

Examples

Here are a set of small examples of how collections can be declared and used:

C_COLLECTION(myCollection)
col1:= New collection(12;14;"zoulou";45)
col2:= New collection(50;"bravo";col1;Current date)
col3:= New collection(col1;col2)

$obj:=New Object("val";"Echo")
col:= New collection(12;"alpha";$obj;True)
vNum:=col[0] // Access first element of the collection
vString:=col[1]
obj:=col[2]
bool:=col[3]

$n:=col.length // This is the way to know the number of items of a collection!
vString:="The collection contains "+String($n)+" items"

Roland Lannuzel
• Product Owner & 4D Expert •After studying electronics, Roland went into industrial IT as a developer and consultant, building solutions for customers with a variety of databases and technologies. In the late 80’s he fell in love with 4D and has used it in writing business applications that include accounting, billing and email systems.Eventually joining the company in 1997, Roland’s valuable contributions include designing specifications, testing tools, demos as well as training and speaking to the 4D community at many conferences. He continues to actively shape the future of 4D by defining new features and database development tools.